I'm using OneSignal for my Android notifications. I am able to receive the notification and start an activity in my own NotificationOpenedHandler():
String code = payload.additionalData.getString(Cons.KEY_CODE);
if(code.equalsIgnoreCase(Cons.NOTIFICATION_DETAIL_SCREEN)) {
Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
startActivity(intent);
}
The problem is that if I'm in another app and I click the notification, my app will not be brought to the front. Although when I navigate to my app, the activity I specified above has launched as intended.
I've disabled the default launcher in my manifest as well:
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
Anyone know why my app is not being brought to front?
Try setting appropriate flags with intent intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
StartActivity call should be preceded by context which is not there in your mentioned code. You can try that too - i.e getApplicationContext().startActivity(intent); without that startActivity won't get resolved
You need to put this in your intent before start the activity:
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Then start the activity.
Related
In my app I always want user to start from Splash screen. For example, my app may be open in background and some notification pops up which starts splash activity. This should terminate all previous activities which were running.
I have accomplished this by storing list of all running activities references. And when splash activity starts it just calls
for(runningActivity : runningActivitiesList) {
runningActivity.finish();
}
This solution works well. However, Android Studio gives me warning of memory leaks when storing references to activities.
Can someone please suggest me a better approach which avoids memory leaks?
Maybe enough is to start Activity with clear stack:
Intent intent = new Intent(context, clazz);
intent.setFlags(IntentCompat.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Tried all other options, but only thing worked for me is:
final Intent intent = new Intent(applicationContext, SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
return IntentCompat.makeRestartActivityTask(intent.getComponent());
Please NOTE: This solution is also not full proof. Since, when I open my app through Google Play Store it launches splash activity even when another instance of app is running in background. Thus I end up having 2 instances of the same activity.
You don't need to finish all running/previous applications.Instead you can start your activity using TaskBuilder api to handle proper back navigation.
Open your activity with this:
private static PendingIntent makePendingIntent(#NonNull Context context, #NonNull Intent resultIntent) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack
stackBuilder.addParentStack(YourActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}
In your manifest file define the parent activity of YourActivity.class as:
<activity
android:name=".YourActivity"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
</activity>
Follow these urls for more details: http://developer.android.com/reference/android/support/v4/app/TaskStackBuilder.html http://developer.android.com/guide/components/tasks-and-back-stack.html http://www.programcreek.com/java-api-examples/index.php?api=android.app.TaskStackBuilder
In android manifest set:
android:launchMode="singleTop"
For notifications generated from your app you can use #mac229's flags in #Nischal's pending intent.
I'm using the following intent with a notification and the issue is that if the current activity is the same as the intent nothing happens. How do I open the same activity with the new data?
intent = new Intent(context, PackViewActivity.class);
intent.putExtra("pid", pack_id);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Here is the details fromt he Manaifest:
<activity
android:name=".PackViewActivity"
android:configChanges="orientation|keyboardHidden|screenLayout|screenSize"
android:launchMode="singleTask"/> //I've also tried singleInstance with no success.
Currently everything works fine unless the current activity is PackViewActivity. In that case nothing happens.
Like #Vivek mentioned, use Intent.FLAG_ACTIVITY_CLEAR_TOP and remove Intent.FLAG_ACTIVITY_CLEAR_TASK. Now, if your activity is already running the new intent will be delivered in onNewIntent(). That is where you should put your intent reading code. Also, get rid of android:launchMode in your activity manifest description since it brings a lot of problems with it.
If the required activity is already in foreground then you need not to push a notification. Alternatively you can register the activity as a listener to the service or activity class which pushes the notification through NotificationManager. Then handle the notification appropriately.
I have a PreferenceActivity with a custom DialogFragment for clearing the app data, so I want it when the user clicks Yes, the application to either close completely (finish all activities) or just to tell it to go to the InitialSetup activity, so the app ca be set up again anew. So far I havent been able to do it in any way...
Tried with
Intent intent = new Intent(context.getApplicationContext(), MySettings.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
but that still does not close all the activities in the back stack...
How can I do it?
What you need is FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flags for your intent:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Or as mentioned Sartheris use IntentCompat for Android APIs below 11:
Intent intent = IntentCompat.makeRestartActivityTask(new ComponentName(context, MySettings.class));
startActivity(intent);
If you only want the app to close, with the user having to start it again later, you can use
System.exit(0);
as you would in ordinary Java.
If you want your app to restart automatically after it's closed, use a PendingIntent as the answer for this question does:
how to programmatically "restart" android app?
When user press "Home Button" my app go to Background, but is still running. I need bring my app to front again. For example i've this code:
Context ctx=getApplicationContext();
Intent i =ctx.getPackageManager().getLaunchIntentForPackage("com.example.test");
ctx.startActivity(i);
but this code is to open app in different activity, there are a method or something to do this "correctly"?
You can simulate the "launching" of the app the same way that Android launches the app when the user selects it from the list of available apps. If the user starts an application that is already running, Android just brings the existing task to the foreground (which is what you want). Do it like this:
Intent intent = new Intent(context, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // You need this if starting
// the activity from a service
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
(Source: https://stackoverflow.com/a/12075313/3529926)
You can set this flag to your intent
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
and set the launch mode of your activity to single task in manifest (add this is your activity tags):
android:launchMode="singleTask"
I have a Fragment F. After getting result from a service, F needs to finish the activity it is in and launch a page in the web browser which I do using an intent.
It does seem to work fine if the user presses the back button. However, if he launches the app from recent apps, the activity isn't finished.
I have thought about otherways of doing it. Like finishing the current activity and opening the page from the parent activity. But I'll have to make a lot of changes in the flow. So that would be my last option. Is there any way to make sure that the activity is finished even when I launch it from recent apps?
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
getActivity().finish();
Edit: Added code.
try this code for start browser and clear all the stacks of your application
Intent intent = new Intent(); // need to set your Intent View here
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getActivity().startActivity(intent);
getActivity().finish();
Updated
Try this attribute in your activity in AndroidManifiest.xml file
<activity android:name=".MainActivity"
android:excludeFromRecents="true" ...
use the following code
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();