Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a single activity instance but be able to update the view?

In my situation, there is one case in which I need to make sure the activity only runs one at a time.

I found if I set the LauchMode of the activity, I can reach the single instance aim, but it won't update the view of the activity.

This activity is launched by startActivityForResult, and we send the URI with the intent to the activity.

Let's discuss with this certain case:

  • gallery - lauch this activity with imageA.

  • camera - lauch this activity with imageB.

My request is not to destroy the old activity, but the activity that just received the new intent infomation should refresh the view.


I found a new method, onNewIntent. This method can refresh the intent before resume. I will try it.

like image 264
johnsonYu Avatar asked Nov 15 '11 07:11

johnsonYu


People also ask

How to make activity single instance?

android:launchMode= "singleInstance" /> ;this method will start a new task with the activity. A "singleInstance" activity, on the other hand, permits no other activities to be part of its task. It's the only activity in the task. Thus, use android:launchMode="singleTask".

What is single instance launch mode?

singleInstance This is very special launch mode and only used in the applications that has only one activity. It is similar to singleTask except that no other activities will be created in the same task. Any other activity started from here will create in a new task.


1 Answers

You can have an Activity with a manifest attribute of singleInstance. As soon as the activity is relaunched , onResume gets called. You can update the view with the new image and invalidate the older View.

<activity ..       android:launchMode= "singleInstance" /> 
like image 200
Rajdeep Dua Avatar answered Oct 12 '22 07:10

Rajdeep Dua