Using actionbar home as up button, home activity saveInstanceState is always null

后端 未结 2 1109
Happy的楠姐
Happy的楠姐 2021-02-03 19:52

Activity A ===click button===> Activity B

When press back button, Activity A is not recreated.

When press home as up button, Activity A is recreated.

So

相关标签:
2条回答
  • 2021-02-03 20:19

    In the onCreate() enable the home button.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
    

    In the onOptionItemSelected() method do this.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    This should enable Up navigation. If you want the parent activity to be restored with a savedInstanceState. You should set launchMode="singleTop" in the parent activity in Manifest file.

    For more info check out http://developer.android.com/: Providing Up Navigation

    0 讨论(0)
  • 2021-02-03 20:29
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    I used finish() insteed of NavUtils;

    0 讨论(0)
提交回复
热议问题