0

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.

2 Answers2

2

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.

j2ko
  • 2,479
  • 1
  • 16
  • 29
  • Hey are you sure about the fact that further requests should be handled in onNewIntent() ? – Filipe Gonçalves Sep 04 '16 at 15:43
  • `onNewIntent()` is meant as entry point for `singleTask` activity which already running and therefore would not call `onCreate()`. – j2ko Sep 04 '16 at 16:31
  • I am having a problem, the method onNewIntent() is also called in the 1st time right ? At least that is what is happening to me. It is running the onCreate() and the onNewIntent() – Filipe Gonçalves Sep 04 '16 at 22:18
  • No, `onNewIntent` get called only if activity already exist. Maybe you sent few `Intent`s in a row? – j2ko Sep 04 '16 at 22:58
  • I do send them in a row, but it is going to the onNewIntent() right at the first one. I know this because i put some system.out.prints... – Filipe Gonçalves Sep 04 '16 at 23:01
  • I've just tried on emulator and can't recreate such behavior. On the first intent only `onCreate()` get called and on the second intent - only `onNewIntent()` – j2ko Sep 04 '16 at 23:07
  • I think it is a problem on my code, because before that i connect to a network, and i use the method reconnect(), so it connects, and then reconnects again. The thing is that i am triggering the intent (for the 1st time) inside a broadcast receiver that checks if i am connected to the network, since it connects, it starts that activity, and then it reconnects, it starts it again. So that is the problem that i have. Your answer is right. Btw, do you know how i could solve this problem of throwing 2 times the intent inside that broadcast receiver? Maybe a bool as a flag – Filipe Gonçalves Sep 05 '16 at 13:48
  • Hey i was looking at this again, the activity B will be relaunched from a Service, so i gotta put Flag_Activity_new_task to start that activity. But apparently nothing is working because the extras that i put in the intent become null :( – Filipe Gonçalves Sep 27 '16 at 11:30
1

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.

Ganesh Kanna
  • 2,269
  • 1
  • 19
  • 29