i am making a chatting application for android, now i create a new activity when a button is pressed to start the chatting. when i hit the back button the chats dialogs will disappear as the activity is destroyed and on the button click a new activity is always created.
i want to save whats happening in my activity ( chatting history ) as well as the ability to open multiple chat windows, on click of button i can create new chat activity with the others not destroyed ..
I appreciate your help
Preferred way: Don't save the state of the chats in the current activity - bind some Service to the activities and load information from it.
Alternative: If this seems harsh for you, then you can use some Singleton to save all the information (which I not recommend, singletons are evil).
Second Alternative: In onPause of the activity, you can save the current state of the chat in SQLite database. And after that, in onResume you can load it again.
Related
I was wondering if it is possible to save multiple instances of activities in a list as in when a user selects an item in a listView it opens up its corresponding activity that was activated earlier.
To give you a sense of where I am coming from here is the concept of the app I am making.
In Main Activity there is:
A button that when pressed launches a new activity called Exercise
A fragment WorkoutsFragment that keeps track of a list of prior workouts.
In Exercise Activity (whose parent activity is main activity):
Users can input what exercises they are doing in the activity.
When the user goes back to main activity a new item is added to the list in WorkoutFragment and that item contains a summary of the exercises done in the prior Exercise activity. When this item is pressed I want it to go back to the Exercise activity created earlier with all information retained.
So if a user presses the workout item for June 12 I want to to resume the Exercise Activity that was created on June 12.
Would it make more sense to just start a new exercise activity and populate its contents from a database?
You are thinking right. The right way to do it is to create a new Exercise activity and populate its content from database. Maintaining references to activities is not a good practice.
You can also maintain a cache of recent exercises accessed to avoid the database call.
This seems like a good case for using the Activity onRestoreInstanceState.
See this guide on restoring different states of an Activity:
https://developer.android.com/training/basics/activity-lifecycle/recreating.html
You may pass basic data between your Activities through the Intent extras.
I have three top level activities in my application. Activity A, B & C.
Each one of these activities hosts a navigation drawer. I am trying to figure out the best way to manage the activity stack between these three activities.
For example, When I start the application, Activity A is launched.
Activity A has a navigation drawer like Activities B & C. When I click on Activity B in the drawer, Activity B is launched and clicking on Activity C in the drawer launches Activity C etc...
I don't want to finish these Activies when the drawer launches a new Activity because they load data from a backend service, and when I click the back button I want it to send the application to the background.
Essentially, I am looking for a way to launch the activity if it does not exist, and if it does, just resume it. How can I accomplish this?
I think decoupling retrieving data from the activity is the best option.
The following paragraph is from Tasks and Back Stack:
Because the activities in the back stack are never rearranged, if your application allows users to start a particular activity from more than one activity, a new instance of that activity is created and pushed onto the stack (rather than bringing any previous instance of the activity to the top). As such, one activity in your application might be instantiated multiple times (even from different tasks), as shown in figure 3. As such, if the user navigates backward using the Back button, each instance of the activity is revealed in the order they were opened (each with their own UI state).
So in your case, grabbing the data in the background when the app starts using async tasks and storing them in the database might work out better.
One way to do it would be:
On create of the home activity, quickly grab the home activity's data via async task while showing a progress bar. When done, store it, and display it. Then, launch async tasks for the data for other activities. There are some conditions that could be tricky. For example, you have to make sure you show a progress bar if the user quickly switches to Activity B or C before your data is ready.
Perhaps using a singleton might suite your needs if you do not want to use the DB. Depending on the size of your data, parceling your data and passing it through a bundle might also prove to be a good technique.
I am working on an App for collecting Customer Details like personal, business, assets etc.
The app shows a ListView with options and based on options Activities are started. After entering details in personal user presses back button and returns to MainActivity (Activity with a ListView) and selects another option like business details and assets detail. User enters the details and come back to MainActivity by Back button and selects another option.
I tried to save my Activitys using onSavedInstance(Bundle) and onRestoreInstanceState(Bundle) and sharedpreference but failed.
Can anyone help? I will provide code if required.
OnSaveInstanceState()/OnRestoresInstanceState() is not supposed to be used on a back key pressed from an Activity. In fact , OnSaveInstanceState() is never called. Here the user is explicitly destroying the Activity and removing it from the back stack.
These two methods are used in case where user presses home button or in cases where Android System destroys your Activity.
The options you can try
Try using StartActivityForResult()/OnActivityResult() methods. Here the target activity can set data whcih can be accessed by your parent activity.
Use an application class(a single instance class (singleton class) for holding data).
Things like shared SharedPreferences, files, database, etc.
How do I save previous activity and have that update into a listview so that when a user launches the app at a later time he can click on a menu option called history which will bring a listview of the last few activities. Let's say the listview holds 5 activies and after the 5 activies it deletes the oldest activity saved and saves the new activity in it's place.
For example if someone wants to view history of an android activity that he viewed two days ago, he can simply click on a button called history and view his past 5 entries which will be displayed in a listview.
Any ideas?
You can't do it by default Android Activities method.
Just save state in onResume/onCreate (as file in filesystem or SharedPreference) and read it from yours History Activity.
Sorry for bad english)) Ask if don't understand something.
I have two buttons on my application. One button starts a new Game (lets say solitair).
When ever this button is pressed it will always start a new game
My problem is that I would like a second button to go back to the game that has already started if the user clicks back.
How do I go about recalling that activity that has already been created
Thanks
I assume that the game is from a third party. As I see it you don't even need second button. Just click the first one again and you will get back to the game.
If the game intercepts Back it may intentionally destroy it's state so there is no way to jump back to the place that you were before hitting Back.
You have to save the state of the activity
onSaveInstanceState() and onRestoreInstanceState() are used to handle the activity state
onSaveInstanceState() method gets called for an activity when user leaves the activity. Note this method is only called when activity is present in the History state and user can come back to the activity.
onRestoreInstanceState() restores the state of the views.
see the following link
How to manage activity state
You have to read manual about activity stack
Your application can has more than one task and more than one activity back stack