可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm using the Actionbar and it's "up" button to return from a detail activity to the main activity, which works fine. Similarly, the user can press the system "back" button to return to the main activity.
In my main activity, in onCreate()
data is downloaded from the internet to display upon app start. I noticed that when I use the Actionbar "up" button to go from detail to main activity, onCreate()
is run, re-downloading the data. But onCreate()
is not run when I use the system "back" button, therefore immediately showing the main activity view.
The code I use in the detail activity to implement the "up" button is:
switch (item.getItemId()) { case android.R.id.home: Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); return true;
I would like the "up" button to behave like the "back" button and not rerun onCreate()
. But I'm unsure how to make this happen, or which code path the "back" button implements to return to the main activity.
Thanks!
回答1:
Instead of starting a whole new activity simply finish the details activity you are in
switch (item.getItemId()) { case android.R.id.home: finish(); return true;
Then you will return to the previous activity on the activity stack (your main activity) and onCreate shouldn't be called
回答2:
If you want Up to do exactly what Back does, you can do this:
@Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case android.R.id.home: onBackPressed(); return true; default: break; } return super.onOptionsItemSelected(item); }
Note that the default implementation of onBackPressed()
just calls finish()
, but onBackPressed
can be overridden.
回答3:
I think a better solution can be found in this post.
Calling finish()
works in specific situations but may not always produce the behavior described in the documentation e.g:
By calling
Intent intent = NavUtils.getParentActivityIntent(this); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); NavUtils.navigateUpTo(this, intent);
you'll return to the parent activity in the state in which you left it. If you have a flat app structure, it'll still act just like the Back button.
回答4:
for a real "home" functionality , you should see the api demos ,under "App/Activity/Reorder Activities" .
the reason : what if you have something like this : activity1->activity2->activity3 , and now you wish to go to activity1 by pressing the home button on the action bar?
回答5:
I believe the simplest way is to override the "getParentActivityIntent" method of the detail activity adding the flag "Intent.FLAG_ACTIVITY_CLEAR_TOP":
@Nullable @Override public Intent getParentActivityIntent() { Intent intent = super.getParentActivityIntent(); if (intent != null) { return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); } return intent; }
回答6:
There is another solution which can be in hand for somebody. I had the same double-behavior when user pressed Back and ActionbarBack buttons. I was fine with Back btn behaviour. I didn't want an activity to be recreated. So I overrode the method
@Override public boolean onSupportNavigateUp() { onBackPressed(); return true; }
Works fine for me