4

I created a base activity that has a menu, this menu will open other activities in the application.

public class BaseActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    //Intent i = new Intent("com.app.Activity1");
    //startActivity(i);

     }

@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.icon:     Intent i1 = new Intent("com.app.Activity1");
                            startActivity(i1);
                            break;
        case R.id.text:     Intent i2 = new Intent("com.app.Activity2");
                            startActivity(i2);
                            break;
        case R.id.icontext: Intent i3 = new Intent("com.app.Activity3");
                            startActivity(i2);                              
                            break;
    }
    return true;
}
}

All activities extend this base activity, so when you press the menu button, the menu pops up and you can select an activity.

However, lets say I use the menu to go to activity A. Once I'm in activity A I can use the menu to go to Activity A again. I can do this X times, but now the back button will go back to the same activity X times.

How can I tell if the activity is already running so a user can't keep opening the same activity?

Or rather, would you suggest I disable the menu item once in activity A?

Thanks for your input. Sorry if this seems like a trivial question.

M Jesse
  • 2,213
  • 6
  • 31
  • 37
  • The answer you selected as "best" will get you into a lot of trouble (IMHO). See my comment to that answer and please have a look at my answer. – David Wasser Aug 02 '12 at 14:52

4 Answers4

2

I think u need not to do that.I think you just appoint your activity's launchMode="singleTask".Code in your AndroidManifest.xml:

<activity android:name=".com.app.Activity1" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait"></activity>
<activity android:name=".com.app.Activity2" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait"></activity>
<activity android:name=".com.app.Activity3" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait"></activity> 

^-^

enjoy-writing
  • 520
  • 3
  • 4
  • This worked perfect. However since my base activity opens up the first intent, how can I prevent the back button from closing that intent and going back to an empty base activity? Expected behavior would exit the program when no other activities are open instead of going to a blank base activity. – M Jesse Aug 02 '12 at 05:58
  • I think this worked, I inserted this at the bottom of my onCreate() ..... if (isTaskRoot() ) { finish(); } – M Jesse Aug 02 '12 at 06:42
  • This solution is horrible. You will end up having 3 separate tasks with your 3 separate activities in them. Each one has the same icon and the same name. You won't be able to tell them apart in the "recent apps list" (long press on HOME). Also, if you are in one of your activities and the user presses the HOME button he won't be able to get back to the activity properly. Please **don't** use different launchModes to solve this problem. – David Wasser Aug 02 '12 at 14:50
2

There is a simple solution to this. You want to disable the menu item for the current activity in onPrepareOptionsMenu() in your BaseActivity like this:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // Determine what activity I am and find the menu item for that activity
    MenuItem menuItem = null;
    if (getClass().equals(com.app.Activity1.class)) {
        menuItem = menu.findItem(R.id.icon);
    } else if (getClass().equals(com.app.Activity2.class)) {
        menuItem = menu.findItem(R.id.text);
    } else if (getClass().equals(com.app.Activity3.class)) {
        menuItem = menu.findItem(R.id.icontext);
    }
    // Disable this menu item
    if (menuItem != null) {
        menuItem.setEnabled(false); // Make it non-selectable (even with shortcut)
        menuItem.setVisible(false); // Make it non-visible
    }
    return true;
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • This is good for disabling the menu item, but it doesn't finish the previous activity so the back button has the history all activities. – M Jesse Aug 06 '12 at 20:47
  • android:noHistory="true" is what I added to an in my manifest so the back button doesn't go through each activity I clicked previously – M Jesse Aug 06 '12 at 21:02
  • Sure. If that's the behaviour that you want. Glad you were able to solve the problem. – David Wasser Aug 06 '12 at 21:21
0

Kind of a trivial solution - why not have a global int in your baseActivity called 'int choice'? When you switch to an activity just set choice to that activity. For example

case R.id.icon:
   if (choice != 1){
      startActivity(i1);
      choice = 1;
   }
   break;
Valentin
  • 1,731
  • 2
  • 19
  • 29
0

You can implement this in two ways:

  1. To call activity A from activity A, call it with flag as FLAG_ACTIVITY_REORDER_TO_FRONT, so A will be called again without creating a new instance, so goin back will not create any problem.

  2. As you said, disable the menu item for activity A if activity A is in front.

Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61