I click button home or recent application to close app and then I open app from recent application. Now I want if time I close app larger 5s my app will restart. How to do it? thank everyone
You can achieve this by SharedPreferences and implementing own logic, like when you will press home button store the current time in onPause() method and when the user opens the app again then check with the condition with the current time in onResume() and the stored value, if its more than 5s then restart it.
One thing remember, when restarting the activity store some negative number in SharedPreference so that, it won't restart again. And to restart the activity write below logic.
Intent intent = getIntent();
finish();
startActivity(intent);
Related
I have an android app which is experiencing a bug after updating to a new version from the google play store. This bug only occurs on certain devices. On some devices (like my Nexus4) I don't see the bug. So let me explain the problem on devices that don't work.
For simplicity, let's assume my app has 2 activities, ActivityA and ActivityB. Let's assume ActivityA is the launch activity. Let's also assume there is a button on ActivityA that creates a new Intent to ActivityB (while keeping ActivityA in the task stack, so clicking the "back" button returns to ActivityA).
On certain devices, I noticed that after updating to a new version from the google play store, the app seems to launch a new version of itself (or a new task stack, not sure how to determine exactly what's happening).
My intention is to return a user to where they left off after an update. So if they were on ActivityB and they click the app icon, I want them to return to ActivityB (this happens with no intervention on my Nexus4, but doesn't seem to occur on other devices).
To deal with this issue, I put logic in my launch activity to go the activity the user was last using. I accomplish this by storing a variable in SharedPreferences on each activity's onStart() method. In the launch activity's OnCreate() method, I check to see if this SharedPreference variable is set, and if so, check if it's set to something other then the launch activity. If it's different, I create a new Intent to the activity referenced in the SharedPreference, and start that activity.
The problem I'm experiencing is that (on certain devices) it appears to launch a separate task stack when I do this. Here's what I see:
User starts app (launches ActivityA)
User clicks button (launches ActivityB)
OnStart() of ActivityB stores "ActivityB" in the SharedPreferences
User updates app to newest version on google play store (clicks the google play store icon, selects "My Apps", and clicks "Update All")
User clicks app icon shortcut from home screen on phone
Launch activity detects SharedPreference was set to ActivityB, so it creates a new Intent for ActivityB (this is the first thing the user sees)
User clicks back button and sees ActivityA
User clicks back button and sees ActivityB **
User clicks back button and sees ActivityA
User clicks back button and sees android home screen (app is shut down)
** here is where I would expect to see the android home screen, but it appears another another version of the app, or another task stack is still running.
How can I make sure there are no other tasks running after updating to a new version from the google play store?
I was able to solve this by setting:
android:launchMode="singleTask"
on my activities in the manifest.
Each time I try closing my android application. I notice that when I close the close key on my android emulator close to the menu key. My app would display all the view that I have visited . It kind of weird to cos there is a default page that I expect that when the user gets to if the terminate key is pressed it should close. Well I am suppose to always call the finish() to prevent android from remember every activity that as been viewed when running the application.
Please how to I go about this.
use
startActivityForResult(intent, 0);
Instead of
startActivity(intent);
then implement "onActivityResult" and finish the app from it by calling
finish();
and do that for all your sub activities.
In my application when i install it from APK, and start it , and press home button to redirect it to background, than again open the application through its app icon, than it restarts from the begening. Means application restarting when we go out through "home button click" and come back to application
How to stop this please let me know.?
I dont want to restart my app when I go out thru home button click, the app must restart only after when I exit from application.
An answer can be found here: Android application restarts when opened by clicking the application icon
It can be fixed by adding this piece of code in the onCreate() method of your first Activity:
if (!isTaskRoot()) {
finish();
return;
}
The bug is explained here: https://code.google.com/p/android/issues/detail?id=26658, and the fix is given in comment #14 :)
First, there is no "exit from application". Then read on activity stack here to really understand how the activity is managed.
I have three activities, lets call them Act1, Act2 and Act3. There is a login form on Act1 which (upon successful login) goes to Act2 which has two options (1. Go to Act3, Go to Act3 with some extra data). The user then goes to Act3.
Of course, when the user presses the "home" button the android device, the application is minimized and held in memory until android needs to use the memory (in which case the App is destroyed). However, when the user presses the "home" button and then opens the app up again quickly, the app is restored to the Activity that was in the foreground before it was minimized.
I want to be able to minimize the app, and then once re-opened go straight to Act1 to prompt the user to login again. I do not want the app to be able to resume in Act2 or Act3.
Unless your application is really security-sensitive, the default behavior should be better for the user: typing their login and password every single time they launch the app can be very annoying. Take for example the native GMail application: it doesn't require you to reauthenticate every time it opens.
Now, if your application really needs that behavior (say it's a credit card safe or something like that), then my first guess would be to handle Act3's onPause() and call finish() from there. Just be careful not to call finish() twice (see isFinishing()).
Also, since this is a breakage of the user's expectations, make it clear to the user that your app behaves like that for their security, not because it wants to be annoying.
When the user moves away from your activity (pressing the home button for example), the onPause() method is called first. You should be able to handle your logic there (for example, calling finish() on Act2 or Act3).
Edit: heh, yeah, what he said :D
i am working on my App. in between i pressed the Home button on the device. my app will go to the background tasks. After that i long press the home button it will show my app as a persistent state. i.e where and what i had done in my app. But i click my app in the directory window it restarts my app. i want to do if my app is in the background tasks it will wake up else it will start. how to achieve that? Any Idea?
When you do that Android will call:
onRestart()
onStart()
onResume()
You can override that methods in order to do what you need. I wouldn't recommend it, though.
Remember that if the device needs memory, it will kill your process and when you open your app again Android will call the onCreate() method of your Activity.
Check the Activity's lifecycle.