Filipe Gonçalves Filipe Gonçalves - 1 year ago 53
Java Question

Re-use the same activity

imagine this stack situation :
A - B- C - D - B , A, B and C are activities, but D is a service.

Basically, i have a service (D) and from that service i want to call an already existing activity (B).

I know that if I want to re-use an activity, all i got to do is to use the flags (or change the manifest) SingleTop (will re-use the activity if it is already on top) or SingleTask (will re-use the activity whether it is on top or not).

The problem is that since i am inside a Service, i will have to add the flag FLAG_ACTIVITY_NEW_TASK, so that i can call an activity. Also, i added SingleTask as a launch mode in my manifest so that that activity will be re-used.

This works fine since it re-uses the same activity and comes back to the method onNewIntent(Intent intent). The problem is that everything that i put as an extra on that intent comes as null. I try to send 2 strings and 2 booleans through that intent and all of them reach the onNewIntent(Intent intent) as null.

How can i solve this ? Do I have to do something inside the onNewIntent(Intent intent) method, before getting the extras ? Are there any better alternatives ?

I heard about the StartActivityForResult or something similar. That would only work 50% of the times, since this is for a " chat-like" application.

So I will be on the "chat", from where I go to another activity where i can select something to send. After that, i will go to the service, where the transfer is done, and then back to the "chat".
But when I receive something, I am already on the "chat", so the startActivityForResult wouldn't work in this case (the service to receive would be running on the background + I don't wan't to finish the receiving part, because i want to be always listening for something).

Here is the code from the service where i try to re-launch the single activity :

Intent transfRec=new Intent(ServerComm.this ,TransferRecordActivity.class);



Here is the code of my onNewIntent(Intent intent) :

protected void onNewIntent(Intent intent) {

System.out.println("I am here in the new intent");
System.out.println("Intent is null inside the new intent method !!!");
System.out.println("The tmpFilePath is : "+tmpFilePath);
System.out.println("O received boolean esta a : : : "+received);

receivedFilePath= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+tmpFilePath;
System.out.println("transfer REcord Activity _: the received file path is :::: " +receivedFilePath);


adapter= new HighwayTransferRecordCustomAdapter(this,listOfItems);

EDIT: As you guys can see i check if the intent is null, and it is not, since it doesn't do the system.out.println that there is on that condition !

Answer Source

The issue is that you are calling getIntent() within your onNewIntent(). From the docs for getIntent():

Return the intent that started this activity.

Therefore, you are getting the intent provided to onCreate(). To get the intent supplied to onNewIntent(), you simply use intent which is provided in the method signature:

protected void onNewIntent(Intent intent) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download