开发者

Android -- Menu items that function as tabs

I have several activities that inherit from a base activity with a menu like below.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
        Intent myIntent;
        s开发者_开发技巧witch(item.getItemId()) {
        case R.id.home:
            myIntent = new Intent(this, HomeActivity.class);
            startActivityForResult(myIntent, 0);
            return true;
        case R.id.map:
            myIntent = new Intent(this, GoogleMapsActivity.class);
            startActivityForResult(myIntent, 0);
            return true;
        case R.id.art:
            myIntent = new Intent(this, ArtActivity.class);
            startActivityForResult(myIntent, 0);
            return true;
        ...
        default:
            return super.onOptionsItemSelected(item);
        }
    }

This works well, but selecting a menu item starts a new instance of that activity. How can pause() and resume() be used in conjunction with setResult() to only have one or less instance of each activity at a given time?

In an effort to be more specific, when a user comes back to a previously visited activity, it should be in the same state as they left it (like tabs).


You could accomplish this by configuring the launchMode of the activities to be singleTask. However, as the docs point out, this may result in unexpected behavior for the user. When the user hits back, they expect to be taken back to the previous screen they were at. If you use this mode, make sure you test the back functionality thoroughly.


Use .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP) to your intent and just start the 'Activity' via startActivity

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜