문제

Im new to android development and have a question about application navigation.

At the moment I have a number of items in an OptionsMenu that drops down in the top right of the app. All of my activities has this same OptionsMenu. Each item in that menu has an OnClick callback that launches a new activity to correspond with that part of the application. This is the way im currently navigating round 'pages' in my application. Code for the onMenuebutton click callback is as follows:

Intent i = new Intent(c, Activity2.class); c.startActivity(i);

The first time an OptionsMenu item is selected its fine, a new activity is created and the app moves to it. However, when I select that item again, a new activity is created, even if one for that option is already running. I can get in the situation where I have a stack of a number of the same activities.

My question is, is there any way to resume an already running activities rather than creating new ones? Perhaps I am doing this the wrong way and instead should be swapping in and out fragments on a single activity?

Thanks

도움이 되었습니까?

해결책

You dont need write extra code.Supose if Activity A is already running,dont want to lunch another same Activity.So you just declare android manifest file like this.

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

Yes of-course the fragment is good options instead of launch new activity.

다른 팁

Then better to go with Fragments.

or

else you can finish your previous activity whenever start the new activity (If you are not having back button)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top