Start Multiple Instances of an activity from BroadcastReceiver - android

I want to create multiple instances of an activity from BroadcastReceiver, the activity contains a AlertDialog, currently I am using the following code for this purpose:
Intent intent = new Intent(this, MultipleInstanceActivity.calss);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
and in manifest file android:launchMode="standard" somehow I think this FLAG_ACTIVITY_NEW_TASK causing the android:launchMode="standard" to change to android:launchMode="singleInstance" or something. I am not able to create multiple instances of this activity. I also tried to use FLAG_ACTIVITY_MULTIPLE_TASK, but no use.
I have created a PreferenceActivity, what really puzzles me is that when this PreferenceActivity is open my app creates multiple dialogbox i.e multiple instances with different data on it. But when its not open, my app wont create multiple instances just to make clear, it wont open another dialog. Logcat is not giving any warnings or errors.
My questions:
How to create multiple instances of an activity from BroadcastReceiver?
Can someone explain me whats happening in the second case, the PreferenceActivity one, why is it creating multiple instances?

I ran into the same problem as you have here, and i solved it by Using both FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_MULTIPLE_TASK.
Intent intent = new Intent(context, YourActivityClass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(intent);
Hope this works for you too.

Related

How to make sure the MainActivity will be created only once

I have a react native project. And there is a service running in the background.
When the app is not running, the service is still alive, and it may want to start the MainActivity in some time. I’m using the following code to start MainActivity(I tried noFlag/addFlag/setFlag):
Intent Intent = new Intent(context, MainActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(key, value);
context.startActivity(intent);
The AndroidManifest.xml is declared like:
<activity
android:name=".MainActivity"
android:launchMode="singleTask”>
</activity>
Each time the MainActivity will be created twice. In the first time we can get the extra value, but the second time it will be empty.
How can I make sure the MainActivity will only be created once?
Thanks.
Just found the problem. My method breakpoint on startActivity worked.
The third party sdk cannot find property receiver, so it send a startActivity call with intent flag FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK, which overrode my singleTask in AndroidManefest.xml.
As you most probably have known, the problem lies with you calling the startActivity twice. I think it would be lovely if you could show the code that calls the startActivity.
I have just now tested that as long as you have singleTask or singleTop as your launchmode, OnCreate wont be called twice. As you can read from here : https://developer.android.com/guide/components/activities/tasks-and-back-stack
the function that would be called is onNewintent and if the putExtra is empty, it indeed can create a NPE
I hope that helps you, Good luck

Pending Intent in Android

The constructor of a pending intent needs an Intent object in it from the current context to the next activity. In my app, I have only a single activity containing multiple Views. No Second Activity.
The Views are Destroyed or made visible on demand. Obviously I cannot add a View to the constructor of an intent. So how shall I direct the pending intent? Multiple views have been accommodated into the main.xml using the <include/> command.
This is very easy to accomplish. mvnpavan already gave you good hint, you just need to do some additional work. First of all you should get familiar with this page http://developer.android.com/guide/components/tasks-and-back-stack.html.
It explains how to manage your application tasks and back stack. In your case you have 2 options:
1.Configure launchMode property of Activity in manifest. Setting singleTop should do the job since your are using just one activity.
<activity
android:name="com.yourpackage.YourClass"
android:launchMode="singleTop">
</activity>
2.Add flag FLAG_ACTIVITY_SINGLE_TOP to your Intent(the one you are passing to PendingIntent):
Intent intent = new Intent(this, YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Please note that in both cases you can implement callback method of you activity onNewIntent(). Use it in case you wish to do some extra work when app is bring back to the foreground.
Update:
Regards keeping connection to the server I would use Service, not Activity. Then you can do whatever you like with your activity. This is the best practise. If you worry that Activity has no direct access to the Service API, you can bind to that Service. Have a look at this page http://developer.android.com/guide/components/bound-services.html You can just copy/paste the code and your are done. It's very easy to maintain. In the future you may need to create another activity which require connection to the server. It will save you a lot of time and headaches :).
Use pending intent to resume your application and not recreate it like this:
Intent i = new Intent(this, youractivity.class);
pIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
Hope it helps! ;)

Start multilple activity from broadcastReceiver

I want to start multiple activities from my broadcast receiver. I have two classes i.e ReadContacts and CallDetails. I want to start them one by one. like first calldetails activity should be started and then next. I have tried below code and it works fine.
Intent calldetails = new Intent();
calldetails.setClassName("com.simplereader", "com.simplereader.Calldetails");
calldetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(calldetails);
then I tried below code to start other activity
Intent readcontacts = new Intent();
readcontacts.setClassName("com.simplereader", "com.simplereader.ReadContacts");
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(readcontacts);
But its not working and application crashes.
You must have the Intent Flag Intent.FLAG_ACTIVITY_NEW_TASK to start an Activity from outside of an Activity context so you need to add that flag to your second Intent.
I don't know if this is your only problem but if that doesn't fix it then post your logcat so we can see the error.
Intent readcontacts = new Intent();
readcontacts.setClassName("com.simplereader", "com.simplereader.ReadContacts");
calldetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // you need this flag
context.startActivity(readcontacts);
FLAG_ACTIVITY_MULTIPLE_TASK Do not use this flag unless you are implementing your own top-level application launcher.
From the android developer documentation for intent.
You could probably just launch both activities with the new task flag.
I think you are making mistake in this line
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
If you want to start readcontacts activity it shoul be
readcontacts.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
instead of
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
I think this is the reason.

More than one instance of activity due to intent

For an App I am developing, I override the back button to make it act like the home button so that the state of the main activity is preserved even when the app is exited. Now, I also send a notification to the user from time to time using a service. When this notification is pressed I want to open the main activity again. I noticed though that this creates a second instance of the app, which creates major problems. I am trying to make the main activity go to the front again, without calling oncreate again like so:
Intent to launch main activity again:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
This doesn't work though. I still end up with two instances of my main activity. Does anybody know how to fix this?
By the way, I already have android:launchMode="singleInstance" in my manifest.
There's a way to force the OS to create only one instance of an activity and thats using the tag launchMode in the Manifest as shown below:
<activity android:name="YourActivity"
android:launchMode="singleInstance"/>
Hope this Helps...
Regards
Try adding this flag to the intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), works for me.

Avoiding multiple instances of an activity [duplicate]

This question already has answers here:
Android singleTask or singleInstance launch mode? [closed]
(3 answers)
Closed 9 years ago.
I simply want that my activity has only one instance. I read about intent flags and launchmodes, but it just refuses to work.
I tried SingleTask, SingleTop, various intent flags etc. etc.
My manifest:
<activity
android:name="com.secret.domain.Player"
android:label="#string/title_activity_player"
android:launchMode="singleTask">
</activity>
and the launch code:
Intent intent = new Intent(getActivity(),Player.class);
intent.putExtra(Intent.EXTRA_TEXT,s);
intent.setData( Uri.parse( s ));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
The Player activity obviously plays music, and still does it if I go back and create a new instance, both can play at the same time. OnNewIntent() is never called by the way.
I assume I've did something wrong, but I cant find out what.
EDIT: I know it sounds similar to other threads, but I read them and still couldn't figure out how to achieve what I want.
So you have made a shot to your leg, he he )))
As your activity should be run in single task (because of singleTask attribute). Every time when you start it, the system creates a new task for your Activity retaining the previous instance and it's media player component in memory.
That's why you hear several music tracks from several created tasks.
In such situation you may either make Activity to be as a singleInstance (using appropriate attribute in manifest), then if you already have created task with Activity in it, you just will need to handle intent in onNewIntent() and you should start it without any Intent flags. But you should make this Activity to have ACTION_MAIN and CATEGORY_LAUNCHER filters as described here in the second paragraph. Or you should make it standard in your manifest and launch it with FLAG_ACTIVITY_CLEAR_TOP considering that you will lose all the top activities.

Categories

Resources