Reputation: 25459
I'm working on my first Android app and I have one issue I cannot sorted. I checked out stack overflow but I cannot find the solution.
I have a menu which show 4 different Activities when menu item is selected. I also have a class which manage the menu:
public class TabMenuManager {
final Context context;
public TabMenuManager(Context context) {
this.context = context;
}
public boolean handleTabMenuAction(int item) {
Log.d("Toolstrea", "TAB MENU HANDLED: " + item);
switch (item) {
case R.id.action_home:
handleHomeAction();
return true;
case R.id.action_reorder:
handleReOrderAction();
return true;
//.....
}
private void handleReOrderAction() {
if (this.context.getApplicationContext() instanceof ReOrderActivity) {
Log.d("Toolstream", "REORDER CLASSES THE SAME");
Intent reOrderIntent = new Intent(this.context, ReOrderActivity.class);
reOrderIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.context.startActivity(reOrderIntent);
}
else
Log.d("Toolstream", "REORDER CLASSES NOT THE SAME");
}
private void handleHomeAction() {
// Simmilar as one above
}
}
In all activities I show the menu I just call:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
TabMenuManager tmm = new TabMenuManager(getApplicationContext());
boolean success = tmm.handleTabMenuAction(item.getItemId());
if (!success) {
return super.onOptionsItemSelected(item);
}
return success;
}
This class just simple show activity but I want to make sure it won't present the same activity as current one. In this example I use:
this.context.getApplicationContext() instance of ReOrderActivity
But I also tried
this.context.getClass() == HomeActivity.class
It always log that the activity are different. It cause the problem that if I'm in HomeActivity I can press Home in my menu and another instance of HomeActivity will be added on the stack and so on.
How can I make sure I present just one instance of the activity? Is there a better way I doing that?
Many thanks.
Upvotes: 0
Views: 1438
Reputation: 9989
In your code, this.context.getApplicationContext() instance of ReOrderActivity
can never be true. You should change it to: this.context instance of ReOrderActivity
You also need to change how you create your TabMenuManager in onOptionsItemSelected. Change it to: TabMenuManager tmm = new TabMenuManager(this);
Upvotes: 2