How can I use the Android back button to move back within my app instead of closing my app?

前端 未结 4 695
迷失自我
迷失自我 2021-01-22 18:31

My app has three activities, A, B and C. I am moving from A to B through an OK button, and I want to move back from B to A by using the default back button of Android devices. W

4条回答
  •  囚心锁ツ
    2021-01-22 19:07

    In my onClick method (in Main Activity) I use the code:

     Intent intent = new Intent(context, SecondActivity.class);
     context.startActivityForResult(intent, SecondActivity.SECONDACTIVITY_REQUEST);
    

    In the manifest I've got:

     
    

    This works for me without any other settings that I can see. What events are you responding to?

    Note that you can also go back an activity, in code like this:

    super.setResult(Activity.RESULT_OK);
    super.finish();
    

    Edit... Make sure you're not swallowing the event in the Main Activitys onKeyDown event.

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //your code here
        //if (keyCode ==
        //...
        //else
        return super.onKeyDown(keyCode, event);
    }
    

提交回复
热议问题