Finish an activity before starting the other one from notification - java

I've implemented OneSignal push notifications in my Android app. I'd sending push notifications with URL as a payload attatched to them. That URL sends an intent that can be opened by another activity of my app or the browser. So, when some activity of my app is in foreground and I choose to open the notification in my app, there are 2 instances of my app. So, even if a user taps Exit in the now top-most activity, the one that was already opened still remains. Basically, users have to exit twice from my app (and possibly more times if this keeps on happening).
So, is there any way by which I can finish the foreground Activity when the user taps on the notification (and chooses to open it in my app instead of the browser) and then proceed..?
Also, I have tried all the launch modes: normal, singleTop, singleTask and singleInstance, all produced the results that they were designed to, but, none could suit my case.
I don't know which code should I include here.
If this can't be done, is there any way to launch all activities of my app (no matter from where they're launched) into the same instance?

For this you will have to start activity from notification by using activity flag like this
yourintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
it will clear foreground activity instance and your new activity instance create on top.

Related

Android Conditional PendingIntent in Notification?

My application currently has two activities: a splash activity which displays the app logo and loads the user’s data from a remote database, and then the main activity that houses all my fragments. I’ve reached a bit of an impasse right now with notifications. I’m using FCM to deliver the notifications and have that working fine, but don’t really know what to do with the pending intents of the notifications. If the app is closed, I don’t want it to launch the main activity since I load the data from the DB in the splash activity. Thus the main activity would have missing data (it currently crashes due to null pointer exceptions everywhere). But if the app is still running, I don’t want the splash screen to be launched again. Is there some type of way to have a conditional intent baked into the notification?
Alternatively, is this just bad practice to do what I’m doing with launching the activities from the notifications? I’m by no means an android expert so there might indeed be a better way. I would prefer to keep my current activity flow however where the splash screen is responsible for the initial loading of data. I was hoping there was some way to just broadcast a message on the click of the notification and if any activities have an active listener then it would just consume that data.
The solution I have come up with is to have a single activity whose sole purpose is to process notifications and then pass them to the appropriate activity either through a new intent (if app is terminated) or through a broadcast (if app is resumed). Not sure if this is bad practice (would appreciate anyone telling me if this violates any type of software principle).
It's working like a charm so far and is honestly really nice to have all that logic in one activity anyway - all the service has to do is pass in the data from the notification and the action and the activity does the rest.

Bring app to the front when notification is clicked

I'm trying to add functionality to my foreground service notification to just open the app when the notification is clicked. I don't want to run any activity - I just want to display the app.
Basically I want to achieve same thing that can be achieved in Android by bringing list of apps and clicking on one).
I've checked lots of posts on StackOverflow, but all of them use activity class in Intent, while I don't care about any particular activity - I just want to bring the app up.
You must have a launcher activity right? so you always can start an activity with your launcher activity and use a launch mode in the AndroidManifest.xml to define to bring it up if it already has been created.

Get notified when instance of app widget was added to homescreen by user

I've developed an android app where you can customize your own widget.
My problem: I want to start an Intent when an instance of the widget is added to the homescreen.
Until now I'm using the onReceive() method, which is called by the system in my AppWidgetProvider class as soon as anything happens. There I wait for an Intent which is sent by the system and which has the intent action
AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED
(onWidgetDataOptionsChanged() is according to android developer page the right method: http://developer.android.com/guide/topics/appwidgets/index.html).
I've tried this on several real devices and it always works. But on all emulators with new android versions, no Intent with the above mentioned action is sent by the system when I add a widget instance to my homescreen.
So, is the issue caused by the emulators or should I wait for another kind of action?

Intent-filter Browsable for and android Application, not Activity?

In iOS, there is a way to register custom scheme, and it is possible to open a whole app, like new instance or one with some app state (running) and it is not just a single ViewController.
For Android i am looking for a way to have whole stack of activity recovered (that might be launched before launching a browser) after user clicks a link that redirects to my app.
I don't want user to have to tap back till he closes the browser and goes back to my app activities, and I don't want to launch just single derived activity - but just go back to my app activity stack with the new one on the top.
Is this possible to achieve in some neat and clean way? Or at least clean activities of the app that were launched before a browser where user clicks a link that opens my app's activity?
Ok, the solution was to create launching activity that would be browsable. This activity would then launch desired activity with flags :
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
and immediately finish itself...

Always resume back at my app's main form/activity

My Android app has a button that executes this code in order to open the app market pointing to a certain app.
It works.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(getResources().getString(R.string.app_url)));
startActivity(intent);
But whenever the user exits my app at that point, by hitting HOME, the next time he reopens it (resuming it, even days later), the app is STILL pointing at the app store.
How do I make any "reopening" always return back to my app's make form/activity?
(Same as if he had hit BACK, instead of HOME, when he exited my app yesterday.)
Thanks.
From google + android developers post.
If your application launches the activities of other applications (such as to view a picture or pick an attachment), there is an important intent flag to know:
Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
Use this when starting an activity that you don't want the user to return to if they relaunch your application from the app launcher or a shortcut on the home screen. For example, it would be confusing to tap on the Gmail icon and find yourself selecting a picture because that is the last thing you had been doing as part of the Gmail task.
Before startActivity(), try this:
intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
If you would like the activity that this is called from to clean up & forget its state as well, call finish() after startActivity() too.

Categories

Resources