minimize application and starts from where user left - android

i am making an application and i want to minimize the application when user click the minimize button and i am using the below code.below code minimize the Application but when i re open the application it always starts with the first activity.And i want the the application should resume from where the user has minimized the application.. i.e. user minimizes application when he is at 3rd activity and when he re open it the application should start with the 3rd activity not the 1st.
Intent main = new Intent(Intent.ACTION_MAIN);
main.addCategory(Intent.CATEGORY_HOME);
main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(main);

Instead of your code try using
moveTaskToBack(true);
Read more about this function.

Related

How do I return to previous app after a service generated activity is 'finished'

My app has a service that runs in the background. It wakes up on intent from the user which is typically generated from another application (such as sharing to my app from the web browser). After the service is done performing a task I want it to open an Activity that displays some info about the task that was completed. When the user is done looking at this info, I want them to be able to hit 'Back' or 'Close' and return to what they were doing previously (not within my application).
I've only managed to get the activity to close leaving my App's main activity front and center on the screen. This only happens if the app is still running. If it's been killed then it correctly returns to the web browser where the user intent was generated from.
How can I make the 'Back' button close the activity and return to the previous app from which this entire workflow was started?
EDIT:
This is the code I am executing from within the android service:
Intent intent = new Intent(this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
When I tap the back button from the TestActivity it returns me to the main activity of my app, not the previous app I was in when the service started TestActivity.
In second line you are using Intent.FLAG_ACTIVITY_CLEAR_TOP which is clearing your previous activity

How to minimise/hide app programmatically?

I have an activity which is purely transparent (user can't see when it gets open).I open this activity from background service whenever I want to open it. After doing some work, I need to close that activity (not finish()).I want to hide or minimize app. Actually, my app opens up for a second and do someWork after doing someWork, it continues to do remaining work (some uploading process) in background i.e. in onPause.I don't want user to know that something opens & closed immediately.
Currently, I am using below code-
public void minimizeApp() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
Now, Problem is that If user is using some other app eg, playing game or using Whatsapp, When my app get opens over previous app(Game or Whatsapp) and minimizeApp() is called then it minimises my app along with other app opened in background.
I want to minimize only my app.
You can try the following from the activity/context from where you want to minimise the app:
YourActivity.this.moveTaskToBack(true);

start an activity without show it

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

Deleting an intent after use

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

implementing login like facebook

I have many activities in my application.
First is=Main.java
Second is=App.java
third is =Location.java
In main.java i have defined that if there is username and sessionid value stored in preferences then directly move to Location activity else show App.java.I have finished main.java in both cases.Main.java is just a splash screen.
I have defined an exit method in main.java that defines:
`android.os.Process.killProcess(android.os.Process.myPid())`
If someone logout from application then it comes on App.java.To close application on back button i have called Main.exit() which closes application in correct way.
And on location page i have used
`moveTaskToBack(true)`
which closes application.
But if i come back directly to Location page after some time my whole application does not work properly its session id expires.
And if i come from App.java page it works well.
I want to create it like facebook if u r logged in and closes the application it starts from second page.and if u logout then shows the login screen.
Please help me to resolve this.
Thanks.
I had something similar in my app. I wanted a way for the user to "sign out" of the application and not just the activity.
So, I did this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
System.exit(0);
That code launches the default home screen (exits the app) and then closes all of my activities. When the application is re-launched, it automatically goes through my MAIN launcher activity (which is login).

Categories

Resources