Widget launching a second instance of application - android

I am having problems launching my application from my widget when it's clicked upon.
Here is a cut of some code
// Create an Intent to launch activity
Intent intent = new Intent(context, Entry_MainTabView.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
updateViews.setOnClickPendingIntent(R.id.Widget, pendingIntent);
Now take the scenario.
I launch my app, exit to the home page , then click on my widget.
It launches a second instance of the application, so when I hit "back" , the application goes away and reveals the other copy. I then hit back again and finally go back to the homescreen.
How can I make the Intent only create a new instance if none already exists in ram ?

Have you looked at singleInstance and singleTask launch modes of an activity? This extract from Pro Android 2 indicates that there is no documented way of ensuring that there is only one instance of the widget running.

Related

Resume existing activity from widget without going through the launcher if activity run in background

I have a widget for my application. This application has two activity: The launcher and the main one.
On click on this widget, it opens the linked app, however for now I'm opening the launcher and then I start my main activity from its launcher activity even if it was already running on the background.
Is it possible, on click, to check if the main activity is already running on the background and if so, resuming it; or opening the launcher otherwise?
There is the code I'm using now to opening the launcher from the widget.
//LISTENER TO OPEN THE APP ON CLICK
Intent clickIntent = new Intent(context, SplashScreen.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, clickIntent, 0);
// Get the layout for the App Widget and attach an on-click listener to the button
views.setOnClickPendingIntent(R.id.widgetstationlayout, pendingIntent);
To bring your application to the foreground in whatever state it was left in, use this:
Intent intent = getPackageManager().
getLaunchIntentForPackage("your.package.name");
startActivity(intent);
If your app is already running, this will bring the existing task to the front in whatever state it was in when it was moved to the background. If your app is not running, this will launch your app from the beginning.
This behaves as if you clicked on the app icon.

How open same activity with different extras from widget

I have working with widgets which helpfull the user to show important data and open the application from widget.
My widget has 3 buttons and i need to open the app when user click on the different buttons.
i need to open same activity but with different data based on which button user clicked. I have passing different extra with intent
for example
Ist button i set user name as "Ranjith"
and for 2nd button i set user name as "Thomas"
and for 3rd button "Rixon"
But when the activity open it always shows Rixon the third one.
logIntent= new Intent(context, MainActivity.class);
earnIntent=new Intent(context, MainActivity.class);
connectIntent=new Intent(context, MainActivity.class);
logIntent.putExtra("user_name", "Ranjith");
connectIntent.putExtra("user_name", "Thomas");
earnIntent.putExtra("user_name", "Rixon");
}
remoteViews.setOnClickPendingIntent(R.id.bottom_of_widget_log,createPendingIntent(context, 0, logIntent));
remoteViews.setOnClickPendingIntent(R.id.bottom_of_widget_earn, createPendingIntent(context, 1, earnIntent));
remoteViews.setOnClickPendingIntent(R.id.bottom_of_widget_connect, createPendingIntent(context, 2, connectIntent));
AppWidgetManager manager = AppWidgetManager.getInstance(context) ;
manager .updateAppWidget(thisWidget, remoteViews);
You have to check the android:launchMode option in your Manifest,concerning the Activity.
As said in the Documentation,
An activity with the "standard" or "singleTop" launch mode can be instantiated multiple times[....]
In contrast, "singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.

finish activity in which a pending intent starts

In my app, i created a custom notification that contain a button that show recent apps(instead of home button long press)
when user open main activity and press home button to go to home screen then drag the notification drawer and click on the button:
Desired result is that the recent apps are shown and the main activity is one of the recent apps.
Actual result is that the recent apps are shown but the main activity is not in them and the main activity resumes.
My code to start "RecentApps (is a dummy class that show recent apps)" class with pending intent
Intent recentAppIntent = new Intent(getBaseContext(), RecentApps.class);
PendingIntent pendingrecentAppIntent = PendingIntent.getActivity(getBaseContext(), 1, recentAppIntent, 0);
notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);
I think that the problem is getBaseContext, so when the main activity is alive and not finished the recent apps are shown but with context is the main activity.
I tried getApplication and getApplicationContext but not working.
I also tried to use flags for "recentAppIntent" but it is not working, It is a half solution to use
recentAppIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
but it not what i need.
So, my question is "How to finish the main activity in which the pending intent starts".
Thanks in advance, Mostafa
You need to ensure that your MainActivity and your RecentApps don't belong to the same task. The "Recent apps" doesn't actually show recent apps. It shows recent tasks.
To make sure that your RecentApps isn't in the same task as your MainActivity, you can add the following to the <activity> definition in the manifest for RecentApps:
aandroid:taskAffinity=""
Also, when creating the notification, add FLAG_ACTIVITY_NEW_TASK to the Intent, like this:
Intent recentAppIntent = new Intent(getBaseContext(), RecentApps.class);
recentAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingrecentAppIntent = PendingIntent.getActivity(getBaseContext(), 1, recentAppIntent, 0);
notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);
I think FLAG_ACTIVITY_CLEAR_TOP will help as a flag.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Explanation for Intent flags are available here: link

Resuming an activity in android from notification

I'm developing an android application, and in one activity, when the user presses the HOME button, a notification appears. Upon pressing the notification the activity would resume from where it left off. The problem is that the activity is recreated each time. Upon pressing the notification onDestroy() and then onCreate() is called. But say if I reopen the activity through the "Recent apps" it resumes successfully.
This question has been asked before on stackoverflow and I have tried the answers provided and none of them seem to work. The API that I'm using is the latest 4.4. The options I have tried are:
Playing around with the Intent flags
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
Setting the activity to singleInstance and singleTask
How can I mimic the intent that is sent when I open my activity through 'Recent apps'?
EDIT: One more caveat that might be important. There's 3 activities in my program A B C. When clicking on the launcher icon activity A is first started. The notification is created only when going from activity C to Home. I'm trying to resume activity C.
Problem solved.
I was debugging the application through LogCat and looking at the intents that were being generated by the ActivityManager. Whenever I was changing the flags of the Intent, I wouldn't see the change in LogCat. So
02-12 11:26:39.586: I/ActivityManager(766): START u0 {flg=0x1000c000 cmp=com.example.app/.controllers.Activity3 bnds=[0,153][1080,345]} from pid -1
Changing the Intent flags didn't change the flg variable in the log. Apart from this, the notification image was staying in the Status bar, my app wasn't destroying it. What I did was add the PendingIntent.FLAG_CANCEL_CURRENT flag to the PendingIntent I was sending:
Intent intent = new Intent(this, Activity3.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(pendingIntent);
So the problem was an incorrect PendingIntent that I must have created before, that was never being removed. Now I'm able to resume the activity by clicking the notification. Hope this helps someone that might have a similar issue.

How do you separate an Android App Widget Activity stack from the Application Activity stack?

I've got an Android App Widget that when clicked uses a PendingIntent to start an Activity, which the main Application associated with the App Widget also uses. After the user is done with the Activity started from the App Widget, and presses the back button, the user is taken to the Activity on the top of the MAIN application Activity stack. I'd like the back button to take the user back to the Android desktop/home screen.
In short, I'd like separate Activity stacks for my App Widget and Application. Not sure why Android wants to combine these.
This is what I've got now in the App Widget and it's not working. Neither Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK seems to have any impact on the stack.
Intent intent = buildWidgetIntent(context, info);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context,
mRequestCode++, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Thanks for any help with this one.
I haven't faced this exact situation, but Intent.FLAG_ACTIVITY_NO_HISTORY is what I needed to make my widget-launched Activity always go Back to the Home screen.

Categories

Resources