Filipe Gonçalves
Filipe Gonçalves

Reputation: 231

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 ?

PS: 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);
                            transfRec.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                            transfRec.putExtra("receivedFilePath",appName+".apk");
                            transfRec.putExtra("joinMode","appselection");
                            transfRec.putExtra("nameOfTheApp",appName);
                            transfRec.putExtra("received",false);

                            transfRec.putExtra("isHotspot",isHotspot);
                            startActivity(transfRec);

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

 protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    System.out.println("I am here in the new intent");
  if(intent==null){
        System.out.println("Intent is null inside the new intent method !!!");
    }
    tmpFilePath=getIntent().getStringExtra("receivedFilePath");
    System.out.println("The tmpFilePath is : "+tmpFilePath);
    received=getIntent().getBooleanExtra("received",true);
    nameOfTheApp=getIntent().getStringExtra("nameOfTheApp");
    isHotspot=getIntent().getStringExtra("isHotspot");
    System.out.println("O received boolean esta a  : : : "+received);
    textView.setVisibility(View.GONE);

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

    getReceivedApps();

    adapter= new HighwayTransferRecordCustomAdapter(this,listOfItems);
    receivedAppListView.setAdapter(adapter);

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 !

Upvotes: 0

Views: 81

Answers (1)

Shaishav
Shaishav

Reputation: 5312

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) {
    super.onNewIntent(intent);
    tmpFilePath=intent.getStringExtra("receivedFilePath");
    ...
}

Upvotes: 1

Related Questions