我正在实现带有 5 个选项卡的选项卡主机。在第 5 个选项卡上,我有一个包含 2 个子活动的活动组。从子活动,如果我按后退按钮,应用程序将返回到父活动。
但是我需要的是按下选项卡按钮,它也必须返回到父活动。
这是我的活动组:
public class Activitygroup extends ActivityGroup {
private Stack<String> stack;
public static Activitygroup grp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
grp = new Activitygroup();
if (stack == null) {
stack = new Stack<String>();
}
push("HomeStackActivity", new Intent(this,Extras.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
@Override
public void finishFromChild(Activity child) {
pop();
}
@Override
public void onBackPressed() {
pop();
}
public void push(String id, Intent intent) {
Window window = getLocalActivityManager().startActivity(id,
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
stack.push(id);
setContentView(window.getDecorView());
}
}
public void pop() {
if (stack.size() == 1) {
finish();
}
LocalActivityManager manager = getLocalActivityManager();
manager.destroyActivity(stack.pop(), true);
if (stack.size() > 0) {
Intent lastIntent = manager.getActivity(stack.peek()).getIntent()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Window newWindow = manager.startActivity(stack.peek(), lastIntent);
setContentView(newWindow.getDecorView());
}
}
这是我在 tabhost 活动中处理第二次按 Tab 的位置:
int numberOfTabs = tabHost.getTabWidget().getChildCount();
for (int t = 0; t < numberOfTabs; t++) {
tabHost.getTabWidget().getChildAt(t).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
String currentSelectedTag = MainActivity.this.getTabHost().getCurrentTabTag();
String currentTag = (String) v.getTag();
if (currentSelectedTag.equalsIgnoreCase(currentTag)) {
MainActivity.this.getTabHost().setCurrentTabByTag(currentTag);
String newSelectedTabTag = MainActivity.this.getTabHost().getCurrentTabTag();
if (newSelectedTabTag.toLowerCase().indexOf("extras") != -1) {
"BACKPRESS FUNCTIONALITY"-MUST BRING THE PARENT ACTIVITY ON TOP HERE
}
return true;
}
}
return false;
}
});
}
默认情况下,苹果手机具有此功能。在按当前活动的选项卡上,它将父活动置于顶部。请建议我一些解决方法。提前谢谢!!
此代码用于从子活动感应 Tab 键按下
int numberOfTabs = tabHost.getTabWidget().getChildCount();
for (int t = 0; t < numberOfTabs; t++) {
tabHost.getTabWidget().getChildAt(t).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
String currentSelectedTag = MainActivity.this.getTabHost().getCurrentTabTag();
String currentTag = (String) v.getTag();
if (currentSelectedTag.equalsIgnoreCase(currentTag)) {
MainActivity.this.getTabHost().setCurrentTabByTag(currentTag);
String newSelectedTabTag = MainActivity.this.getTabHost().getCurrentTabTag();
if (newSelectedTabTag.toLowerCase().indexOf("extras") != -1) {
****Here call a static method to check whether the child activity is active****
childactivity.getappcontext();
}
return true;
}
}
return false;
}
});
}
在子活动中粘贴此代码。如果子活动处于活动状态,则在按下选项卡时将关闭它,因此父活动将可见。
@Override
public void onStart() {
super.onStart();
active=true;
}
@Override
public void onStop() {
super.onStop();
active=false;
}
public static void getAppContext() {
if(active){
System.out.println("gallery");
galleryActivity1.finish();
}
}
如果不清楚,请随时询问。很乐意帮忙。