在我的应用程序中,我有一个选项卡,其中包括两个活动,例如1和2.活动1扩展了ActivityGroup,其中有一个子活动说A.现在,场景变为这样, 在第一个标签中 - >活动1及其子活动A. 在第二个标签中 - >活动2.在第一个选项卡的活动1中执行某些功能后,用户将移至第一个选项卡的子活动A.现在我想做什么,当用户从子活动A按回按钮时,用户应该回到其父活动1.如何做到这一点?以下是我的代码..
public class MainActivity extends TabActivity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources ressources = getResources();
tabHost = getTabHost();
Intent intentProfile = new Intent().setClass(this, Tab1.class);
TabSpec tabProfile = tabHost
.newTabSpec("Profile")
.setIndicator("Profile", ressources.getDrawable(R.drawable.ic_launcher))
.setContent(intentProfile);
Intent intentFriends = new Intent().setClass(this, Tab2.class);
TabSpec tabFriends = tabHost
.newTabSpec("Friends")
.setIndicator("Friends", ressources.getDrawable(R.drawable.ic_launcher))
.setContent(intentFriends);
tabHost.addTab(tabProfile);
tabHost.addTab(tabFriends);
tabHost.setCurrentTab(0);
}
public void switchTabBar(int tab) {
tabHost.setCurrentTab(tab);
}
@Override
public void onBackPressed() {
// Called by children
MainActivity.this.finish();
}
}
public class Tab1 extends ActivityGroup {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(Tab1.this, Second.class);
replaceContentView("activity3", in);
}
});
}
public void replaceContentView(String id, Intent newIntent)
{
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
@Override
public void onBackPressed() {
this.getParent().onBackPressed();
}
}
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
@Override
public void onBackPressed() {
this.getParent().onBackPressed();
MainActivity parentTab = (MainActivity) this.getParent();
parentTab.switchTabBar(0);
}
}
public class Tab2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
}
@Override
public void onBackPressed() {
// this.getParent().onBackPressed();
MainActivity parentTab = (MainActivity) this.getParent();
parentTab.switchTabBar(0);
}
}
在这里,我可以按下后退按钮上的标签2移动到标签1。但是,我无法从标签1的子活动回到父活动。
答案 0 :(得分:0)
您必须覆盖父活动中的后退按钮并从子级拨打电话。有关详情,请点击此链接:Navigate Between Activities in an ActivityGroup
答案 1 :(得分:0)
在Tab1中:
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public void onPause()
{
super.onPause();
finish();
}
Tab1中的