gpt4 book ai didi

java - 从 fragment 返回到上一个 Activity

转载 作者:行者123 更新时间:2023-11-30 09:01:02 24 4
gpt4 key购买 nike

我有一个 FragmentActivity,当用户按下后退按钮时,它会关闭应用程序。

为了尝试解决这个问题,我尝试过:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();

}

return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent intent = new Intent(this, betaEventsActivity.class);
startActivity(intent);
}

问题是这不能很好地工作。它只是将用户带到指定的 Activity ,不一定返回到之前的 Activity 。

我也试过:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//***Change Here***
startActivity(intent);
finish();
System.exit(0);

}

return super.onKeyDown(keyCode, event);
}

但这只会关闭应用程序。

下面是我的 FragmentActivity 代码:

public class usermatch extends FragmentActivity {
private PageAdapter mPagerAdapter;
private FeedbackDialog feedBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_layout);
initialisePaging();
feedBack = new FeedbackDialog(this, "id");


}

private void initialisePaging() {
// TODO Auto-generated method stub
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment3.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment4.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment5.class.getName()));



mPagerAdapter = new PageAdapter(this.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(mPagerAdapter);

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//***Change Here***
startActivity(intent);
finish();
System.exit(0);

}

return super.onKeyDown(keyCode, event);
}

如有任何帮助,我们将不胜感激。

最佳答案

尝试不覆盖 onBackPressed()。

关于java - 从 fragment 返回到上一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26529358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com