4

我创建了一个具有菜单的基本活动,该菜单将打开应用程序中的其他活动。

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;
}
}

所有活动都扩展了这个基本活动,所以当你按下菜单按钮时,菜单会弹出,你可以选择一个活动。

但是,假设我使用菜单转到活动 A。一旦我进入活动 AI 可以使用菜单再次转到活动 A。我可以这样做 X 次,但现在后退按钮将返回 X 次相同的活动。

如何判断活动是否已经在运行,因此用户无法继续打开相同的活动?

或者更确切地说,您是否建议我在活动 A 中禁用菜单项?

感谢您的输入。抱歉,这似乎是一个微不足道的问题。

4

4 回答 4

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> 

^-^

于 2012-08-02T05:48:10.610 回答
2

有一个简单的解决方案。onPrepareOptionsMenu()您想禁用当前活动的菜单项,BaseActivity如下所示:

@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;
}
于 2012-08-02T14:48:08.277 回答
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;
于 2012-08-02T05:49:00.457 回答
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.

于 2012-08-02T05:49:45.310 回答