Filipe Gonçalves
Filipe Gonçalves

Reputation: 231

Activity re-use logic

I have an activity B that is generated from an activity A. In the activity B, i have a button that will send me to the activity C. Inside the Activity C i will run a service and inside that service i want to go back to activity B.

So it would be A->B->C -> service->B.

That B activity is like a chat/history of apk transfer and right now i am still not using a database (later i will change the code for that, i can't right now).

The data that will be shown in Activity B, comes from that service ( i will send apk files) and after the files were sent i want to retrieve to the activity B the name of the app that i sent, so that i can display it on that chat/history.

If inside the service i create a new intent that will run the activity B, it will always start the activity B again, instead of "keeping" the old information that was already there.

How do you guys think i should do this ?

I know that there is a flag that will call the activity B that is already in the stack (and that when that happens it goes to the onResume() method (right?) ), but are there any alternatives ?

What is the best way to do this ?

Thank you guys ! Sorry if it was too confusing.

Upvotes: 0

Views: 116

Answers (2)

j2ko
j2ko

Reputation: 2539

To make ActivityB remain, place following in manifest :

<activity android:name="ActivityB"
   android:launchMode="singleTask" />

This will keep activity. And onCreate will be called only once. All further request should be handled in onNewIntent().

From LocalActivityManager.java startActivity doc:

if the current activity uses a non-multiple launch mode (such as singleTop), or the Intent has the FLAG_ACTIVITY_SINGLE_TOP flag set, then the current activity will remain running and its Activity.onNewIntent() method called.

Upvotes: 2

Ganesh Kanna
Ganesh Kanna

Reputation: 2275

Start Activity C by using

Intent nextIntent = new Intent(this, ActivityC.class);
startActivityForResult(nextIntent, requestCode);

On service you can finish the activity with result code to identify whether it is success or failure. In Activity B receive the result by onActivityResult. If it is success please pull the data from database(use singleton Datahelper class to set/get data as you did not implemented database now. Use Realm which is very easy to integrate and handle).

I guess this will solve your problem.

Upvotes: 1

Related Questions