I created an example project here:
https://github.com/amitishai/Android-notifications
Here is the scenario:
Open app
Press button
Exit app
Click on the notification that was created. When the app opens you will be in Activity "Bla".
Press the OS BACK button.
Long press the OS Home button in order to see the open apps.
Click on the app.
You will see that you have entered Activity "Bla" again, and the text is the same.
If the activity was initially created with the intent, and then destroyed, how is the intent not null when restarting the activity?
The solution was to use Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
Example is here:
Android: Starting app from 'recent applications' starts it with the last set of extras used in an intent
Related
I was trying to start activity from a service without showing it to the user, keep it work in background, I was searching a lot about that, and I found two ways to do that
by starting the activity then start the home main screen like this :
// this is for lunch the app
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.some");
// this is for going back
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// lets do it !
startActivity(LaunchIntent);
startActivity(startMain);
by putting a going back function in the activity itself, like this :
context.getActivity().moveTaskToBack(true);
BUT, in these two ways I have problems
in the first one, if the user was on another app, this operation will close his app and get him to home , more than that, some times the my activity not started but just be in the back without working i.e. if there was a song it isn't played
in second one, when the my activity started a black screen will appear for a second before it back to the home or previous user app
So , simply , this is what I want :
I want a behaviour equal to : the user open my app then he press back button, but without show that the app started unless he see the background apps
how to do that ?
For that, you can use an android Service.
See docs - http://developer.android.com/guide/components/services.html
What I am trying to do is replicate what the ToddlerLock app does. I have managed to clear the default launcher with
PackageManager localPackageManager = getPackageManager();
localPackageManager.clearPackagePreferredActivities("com.android.launcher");
and then open the launch select dialog with this
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
As long as the user checks the "use by default for this action" the home key now sends the user to my app, thus essentially disabling it.
I then use "clearPackagePreferredActivities("com.my_application")" when I exit my app and the user has to choose a new default home app.
My question is how can I choose the default home application (essentially checking the "use by default for this action" check box in code for the "com.android.launcher" package. That way the user does not constantly have to see that dialog box every time they open and close my app.
I think ToddlerLock does this somehow without using clearPackagePreferredActivities
because if I look at the "clear defaults" in the application manager it is not cleared and you only have to go through the set as default dialog box one time on startup and once when you exit to set it back to the normal home screen.
Thanks for your help.
I have implemented the same functionality in different way.
Let's say you have 'LockScreenAcitivity' configured as Home Screen in Manifest.
Launch LockScreenActivity by sending Home Intent.
Android will popup a dialog, to select the default Acitivity
choose your LockScreenActivity from List as default Activity
.....
.....
While closing the Activity don't clear the prefered Activities.
Disable your LockScreenActivity alone by calling PackageManager.setComponentEnabledSetting()
After you disable your LockScreenActivity, android will rollback to previous Prefered Activity (that's your old home screen ).
Next time when you launch your app,
enable your lockscreenActivity again by calling PackageManager.setComponentEnabledSetting()
Launch LockScreenActivity by sending Home Intent.
In my app I have a splash activity which is also the launcher, a main menu activity, a sub menu activity, and a description activity.
When I start my app the splash.java loads and then starts the main_menu intent.
The main_menu intent displays a button; this button then starts the sub_menu intent which displays a different button that starts the description intent.
However, when I click that first button on the "main_menu" android displays a prompt asking to "complete action with" and displays two duplicate names of my app, one works and the other does not.
Also, I only get the prompt on the main_menu activity and not on the sub_menu activity.
Any theories/ideas would be greatly appreciated.
If you have several intent-filters for activities that can process a fired intent then you'll receive this kind of dialog. This means that the fired intent can be processed by several activities (for instance, you can have several image viewers installed in the system and, thus, when you fire an intent to open an image this dialog will appear and ask you which activity you want to open to process this intent)
I am developing an application and its Home Screen Widget.
Now from my widget when i press on a button it would open up my application from where it was left.
Means if i press home button during my application running then my application will go in background mode.
Now i want that it should resume my opened application.
Whenever i press a button from my Widget. How Can i Do it??
Please help
Thanks a bunch in advance!
I've never made a widget before, but this is how I've made a notification launch back into my original activity when you pull down the bar and click it.
Intent originalActivity = new Intent(getApplicationContext(), Widget.class);
originalActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Once again, not sure how you would do this with a widget, but to relaunch it for a notification you convert that Intent into a PendingIntent to be called later when you want to launch back into it. I would assume this is a similar fashion for how you would it on a widget.
I have an odd issue with my application.
I start the application and the activity shows ok.
I then press the home key so that the activity goes into the background. I can see the onPause() method being called.
The application's service then creates a notification which shows on the status bar.
I then click on the notification and the activity is shown and I see that the onResume() method is called.
I then press the home key and the activity goes into the background. I can see the onPause() method being called.
If I now start the application by clicking on the applications icon I see that a new instance of the activity is created rather than using the paused instance.
If I press the home key again the new activity goes into the background.
Starting the application by clicking on the applications icon I see another new instance of the activity is created.
Pressing the back button at the point destroys each activity in return.
What I want to happen is that a single instance of the activity be used.
Any ideas?
Just use the same intent filters as android uses when launches the app:
final Intent notificationIntent = new Intent(context, MessageListActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
As the intent you created to open your activity from notification bar is the same as android used for launching your app, the previously opened activity will be shown instead of creating a new one.
You should look at the different launch modes for Android activities. this should help. Launch modes can be set in the androidmanifest.xml file. I think your solution would be to use the 'singleTop' launch mode.