I understand how to save an application's state by using SharedPreferences, onSavedInstanceState() & onRestoreInstanceState(), etc as outlined in a similar post ( Saving Android Activity state using Save Instance State ), but how do I save the last activity?
To be more specific, my application starts up and goes to a login screen. Once a user logs in and navigates through several activities, lets say he or she leaves the app using the home button or in some other way. Next time the user starts the app, it will go back to the login screen and do a login again. Instead, I want the app to start up and go to the last activity that was on top of the stack when the user left the app in the previous session.
How is the last activity saved so that it can be restored on app startup?
I believe Android does this automatically. We have an app that we are working on. When I click the home button and them come back to the app, it starts in the activity where I left off. We have not written any code to make this happen. It just seems to work.
My colleague has written an article on Android application state including details on getLastNonConfigurationInstance() which retrieves the instance that was last stored. Take a look here: http://www.eigo.co.uk/Managing-State-in-an-Android-Activity.aspx
Please note that the above answer is correct only on one case: If your process does not get killed by android because the resources (memory, ...) are needed for a different reason.
To get what you describe, I would write a custom parent activity and override the correct life-cycle methods to store your application state and read it and act accordingly. Then let all your activities inherit from MyActivity (instead of android.app.Activity)
public MyActivity extends android.app.Activity {
...
#Override
public onCreate(...) {
// Read the Application state,
// check if the currently launching Activity is the right one,
// if not, start the last Activity and finish the current one.
}
#Override
public onDestroy(...) {
// Store the current Activity ID in the SharedPreferences
}
...
}
Take care to call the super.onDestroy and super.onCreate methods in all your Activites (like you should do anyways).
Happy coding and have fun with Android!
Related
Hello our application works perfect but when our users move the app to background, after a while coming back to app its been crashing.
For example i open the app then change three pages then move the app to background. after open from background 30 minutes its crash because its try to load activity when i resumed
Another applications do this from mainactivity, for example: instagram,twitter vs vs.
another applications not to try load resume activity, they are trying to main activity
how can i start my app from mainactivity when user come back to app from background ?
well, the better way would be to identify the null objects and reassign or repopulate them in the onResume() method so that the user can actually return to what they were doing.
if you can't or it's not an option, then try this code in every activity that you do not want to return to.
override fun onTrimMemory(level: Int) {
this.finishAffinity()
}
this code closes the active activity as soon as the app goes into background so when returned, the app is forced to start from the launcher activity.
Although I am not convinced that this is the best approach. there might be something better.
I'm working on an application that connects to a server on a computer, it's about sharing a clipboard, links, etc. I added the activity responsible for sending to the sharing menu so you can send links directly from the browser and here is the problem.
When user select my app I want the activity to be created, sent a link and returned to the browser but without finishing activity.
I want to achieve an effect similar to open recent apps list and select previous app but programmatically.
I tried finish() and onBackPressed() but they close the activity
Finish() will close the activity, as it is it's role. But onBackPressed() should just call onPause. If the activity is killed by onBackPressed() (you should check if onDestroy() is actually called by, for example, overriding it and printing something), then it's possible that you are assigning a flag during the creation of activity that forces it to close later. Can you provide the code that shows the part where you create your activity?
I'm a total beginner with Android and Eclipse and I have few questions that will help me understand the philosophy of Android:
The activity does all the work "behind the scenes". Activities are connected to a layout in the XML file. When the activity is started, if declared in setContentView method, the connected layout will be shown. Activity can be independent, without any layout, it can be invoked by another activity and will do all the work without showing any layout.
Activity is something like a php file which is invoked by my submit button in HTML, and the layout is .HTML which shows elements.
Am I right with this one?
For example, if I want to change the layout of my app, I want to show Layout2.xml when clicking button in Layout1.xml. Then I have to destroy the activity which is connected with Layout1.xml and start the activity which is connected with Layout2.xml? Is this correct? Is there any better way to do this?
How can I (by which method) destroy/stop a certain activity?
Thank you in an advance.
The best bet is to read the Android documentation regarding activites at http://developer.android.com/reference/android/app/Activity.html
I will answer your specific questions here though
An Activity is a window that the user can see (or a hidden window if there is no layout defined). It deals with the logic of a part of the app that the user can see and interact with. If we take the MVC model (Model View Controller), the Activity is the controller, in terms of it controls which data from the Model is shown on the View (the xml layout).
If you want to show a new window/screen/activity you do not need to destroy the current one. You can open a new activity whilst keeping the old one in the background (in the back stack). With the use of fragments, you can have multiple fragments in an activity so rather than changing activities, you can change fragments in a single activity. For more information about fragments take a look at http://developer.android.com/reference/android/app/Fragment.html.
This point relies heavily on the activity lifecycle. When an activity is destroyed, it means it is finishing and this can be done by the user pressing the back button whilst on the activity, the activity calling finish() on itself or by the Android operating system destroying the activity because memory is required elsewhere (this can happen when the app is in the background).
When we say an activity is stopped, it means that the activity is no longer visible to the user. This can be the case where the activity is in the back stack (another activity is in front of it) or if the app has been put into the background.
This is a brief answer to your questions but I highly recommend you read the Android documentation to gain better knowledge.
Hello I require some advice on how to approach this problem,
bear with me as I am still new to android.
I have an activity that opens on application start and requires some information from the user, but next time the user opens the app, i want the application to open a different activity that will display the various information.
Kind of like facebooks app, where when you first run it, you have to login, and only then next time you run the app you are guided straight to the feed.
Any ideas how one could do this efficiently?
UPDATE: Ive stored the information via shared preferences and am now using a controller activity that decides which step to take.
So, Controller activity runs on start up, and decides whether to show a log in screen or whether to go straight to the information. But now im encountering a problem where i end up opening a blank activity (the controller) and then another ontop of that ( the decided activtiy). I dont want the blank activity to show, so its kinda of like a background process, any ideas?
Ideally you would have a main activity like a controller. Use a SharedPreference object to keep track of whether the user is logged in or not. So back in your main activity, read this value and if it is set go to your news feed activity else show a login screen activity. (as you do the check and redirection, you can show a progress dialog)
links for SharedPreferences
MobTuts
Android Developer
I'm currently trying to develop an android application in eclipse(java) which shows some jokes downloaded from a database. The user is able to vote on each joke once, and to make sure they only do that once, I have made a table in the database that contains three columns.
E-mail
Username (Used when a user publishes a joke)
Encrypted Password
I have two "screens" right now:
Login screen
Main screen
At every single start up the application checks the SharedPreferences for a file containing some information, and if there is some information it should load the Main screen, but if there is no account information the Login screen should be loaded.
Any idea on how I can use different screens, and how should it be coded?
Two options:
In your Activity, check if there is account info. If there isn't then setContentView to the Log in screen. Otherwise, setContentView to your other content. If you go this route, you'll have to have the logic of both the login Activity and the other in the same Activity. Shouldn't be too bad if the logic is relatively uncomplicated.
Have two activities. The default Activity can be the login Activity, but in onCreate() you can check if the info already exists and if it does, simply start the other Activity right away and return from onCreate(). Otherwise, continue with setContentView, etc.
You can have a single top layout (e.g. FrameLayout) that contains two overlapping layouts (one for the login and one for the main). Use a single activity. When the activity starts do setContentView() to the top layout. Then define a method selectScreen(boolean isMain) that based on the argument sets the main layout on and the login off (or vice versa). You turn screens on/off using the setVisibility() method in class View. You can switch screen any time by calling that method. If you want to be extra fancy you can use standard animations when flipping screens.