Android: return to main activity problems
In my main activity I have a menu and when a menu option is selected an Intent is created and a new activity is started. When that acti开发者_Python百科vity completes the process should return back to the main activity and all its previous states according to the ActivityLifeCycle.
I notice that when it returns back to the main activity, nothing is accessable and the screen dims. I can only get back to what I expect when I press the menu softkey.
Has anyone experienced this issue before? Feedback would be appreciated! Code sample below:
@Override
protected void onResume(){
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_settings:
return true;
case R.id.menu_decks:
Intent launchDecks = new Intent(this, stackDecks.class);
startActivity(launchDecks);
return true;
case R.id.menu_exit:
this.onDestroy();
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The code in the first post is actually correct(I have something very similar). There is a great chance the error is in the menu.xml res file, even though it inflates without any problems. Check it is fully consistent in both places, and has the same items. I finally solved it after hours of experimenting.
You seemed to be confused with the use of super.
super.m() is used to call a superclass method.
If you inherit the method with no override then super.m() = this.m() See your super.onDestroy
It s absoluetly useless to override a method m() just to call super.m() See your onResume
Sometimes it is usefull to call a super clas method, it allows you to benefit from this code in a subclass. For instance here onCreateMenuOptions is overriden and your subclass can benefit from some imitialisation code for a menu.
Regards, Stéphane
精彩评论