Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

android - How to avoid multiple instances of same Activity?


In my PoC, I have some Activities, HomeActivity, CreateActivity, SearchActivity, ResultsActivity, ResultDetailsActivity, and UpdateActivity.

I have two main navigation paths: Create and Search.

Navigation for the Create path is as follows: HomeActivity--> CreateActivity -(on complete)-> HomeActivity

Navigation for Search is as follows: HomeActivity--> SearchActivity --> ResultsActivity(ListActivity) --> ResultDetailsActivity --> UpdateActivity -(on complete)-> ResultDetailsActivity (with updated data).

Currently, navigation to a new Activity is via startActivity(intent) method. However, this is causing multiple instances of each Activity to be opened.

I'm rather new to Android. Could someone please suggest how I could avoid this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your android manifest, Add to your <activity> tag the android:launchMode="singleTask"

For a full list, check the documentation of activity

In your manifest:

    <activity android:name=".YourActivity"
              android:launchMode="singleTask"
              android:label="@string/app_name" />

Note: don't use singleton.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...