从片段移回上一个活动

时间:2014-10-23 13:35:29

标签: java android android-layout android-activity android-fragments

我有一个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);
}

问题是这不能很好地运作。它只需要用户指定活动,而不必返回上一个活动。

我也尝试过:

@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);
    }

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:2)

尝试不覆盖onBackPressed()。

答案 1 :(得分:0)

覆盖OnBackPressed方法&amp;将寻呼机设置为上一项,如下所示:

@Override
public void onBackPressed() {
    if (pager.getCurrentItem() == 0) {
        // If the user is currently looking at the first step, allow the
        // system to handle the Back button.
        // This calls finish() on this activity and pops the back stack.

        super.onBackPressed();
    } else {
        // Otherwise, select the previous step.
        pager.setCurrentItem(mPager.getCurrentItem() - 1);
    }
}