I have 2 major activity in my project Connect+Details and Menu. After I switch between the first activity to the second everything goes well. the problem start in returning back from 'Menu' to 'Connect+Details'.
when I use the back key to switch between the second activity and go back to the first everything goes well.
problem start when I use this code:
Intent myIntent = new Intent(Menu.this, ConnectActivity.class);
startActivity(myIntent);
I do go back to 'Connect+Details' but every detail in that activity is lost.
my question is simple, how can i go back to previews activity and still have the details used in that activity.
Edit:
the same thing goes even when I am in the 4th, 5th activity so finish() didn't help me.
You probably want to either save your data in SharedPreferences or in a database then use the saved data to repopulate your Activity. You can use SharedPreferences to save a key/value pair when you leave your Connect Activity and this will persist even if your app is closed.
If there will be a lot of data stored then you may consider using an SQLite DB. This can be a little trickier to set up and use but may be worth it if you are storing a lot of data...especially if its for different users.
Both of these options will persist your data when you leave the Activity or the program. You could also use a static Array but one of the first two options will probably be better for you.
The Storage Options Docs has some decent examples to get you started, especially if you decide that Shared Preferences will work for you.
Edit
Unless something has changed, you can't store serializable objects in SharedPreferences. But Here is a SO link that discusses saving them to a file and reading from there. Maybe this will help you.
Don't use Intent to move from 'Menu' to 'Connect+Details' instead call finish(); from it
Only use intent in first class to move to second ie;'Connect+Details' to 'Menu'
Don't call finish() in Connect+Details
Related
I am developing a quiz app in which when the user presses the next button , he gets to a different activity that is the next question . Also it will have a previous button which takes to the previous question . So what I am thinking is to store all the activities in an array(if thats possible) . Later when I need to add or remove the question from the app then I will only have to change the array and delete its activity file .
That is a really bad idea. Instead, in the activity where the user chooses the question activity, pass data through the intent and deal with the logic in the receiver activity itself. Like so:
Intent j = new Intent (this,
QuestionsActivity.class/*whatever your activity's name
is */);
j.putExtra("questionNumber",13/*Arbitrary number*/);
startActivity(j);
Then in the activity....
if (getIntent().getIntExtra("questionNumber") == 13)
//Do what you want here
You should never, ever maintain references to multiple Activities. The Android system may decide at any time to destroy an Activity and your references will no longer be valid.
Instead you should separate the model of questions from the way they are displayed in an Activity. You can send data to an activity, for example some identifier for the question or even the text of the question. Your Activity subclass should be general-purpose in that it can display any question. This allows you to create one activity for all questions rather than one activity for each question.
You can keep an array of questions rather than activities and then use that array to display the correct information as needed.
I have got 2 activities - "A" contains fragment with a list of conversations, "B" represents a conversation (shows it's messages), in B I can also send a new message.
I would like to be able to update A's list of conversations every time a message is sent in B, so when I click android's back button, A activity's view is updated.
The core of the problem for me, is that I'm not starting A through an Intent when I click the android's back button, so I don't know how to get this effect.
Thank you for your help!
When A is on the backstack, there's no guarantee that an instance of A even exists in memory. The answer to the question of how to manipulate A from B is don't.
Some correct ways of doing it:
If your model (the list of conversations) is Parcelable or Serializable, you can pass it between activities via Intent. You can pass it back from B to A if you start B for result and retrieve it from the Intent returned to A's onActivityResult.
Make the model persistent, like in a database or SharedPreferences file.
Put the model in a bound Service. This would be faster than having each activity load it from persistent storage, but you may still need to make it persistent so you don't lose it when the Service shuts down.
I'm new to Android development and was unable to find how to pass values to an activity that is not the next screen that the user will see. If I have activities in this order: 1>2>3 connected by buttons, how do I pass a value from 1 to 3? I'm only familiar with using intents on button clicks, which would require me to pass the value to screen 2 and then to screen 3.
Thanks
There are several ways to do that:
1) From Activity1 you pass data though Intent. In Activity2 you retrieve data and pass it within new Intent to Acitivity3.
2) Save data in SharedPreferences in Activity1 and retrieve them in Activity3.
3) Save data in SQLite database which is default for Android. This is a good option if you need to save/query data not once, but many times.
4) Save in Google Cloud or use other services.
You can see here a full bunch of store options in Android. Without performing those actions, storing/retrieving data though the Intent is the only one way.
Note: Make a static variables is such a bad practice, unless it is just constant values or other utilities.
If the chain pass becomes too complicated then you should persist it. My suggestion is using Preferences to save the value. Later you do an existence check in the retrieving Activity, load it, and if needed clear it. This is the method commonly used when storing app Settings values.
If your values become more complicated in structure and more numerous then you'd need to consider database.
I have a tabhost in my first activity and Im calling another activity sometimes; to handle some details. I need to remember the last tab selected in my first activity when the second one returns. Problem is the first activity is getting destroyed and created again and onSaveInstanceState is not getting called. The only option which I can think of is to write the last tab selected to external memory and read it back, which seems a little extreme considering this is just one tag. Is there any other option? I cant avoid the second activity (I had tried to change that to another fragment--fragment calling another fragment-- but that didn't work so well for me)
Cheers
If you want to save small amounts of data in Android, you can always use SharedPreferences. Here is a good example on how to use it.
And there are some other storage options for Android listed here
I have an Activity which is an OpenGL view. I also have an xml layout to use for preferences. Until now, to show the preference menu, I just brought it to front by setContentView(). And the same to get back to the OpenGL view.
But is this a case where I should give the preference menu its own Activity?
I guess this would make a few things much easier. For example, the back button would just work, opposed to now where I have to code it or it will just exits the application.
And if this is a good idea, how do I pass data both ways? I have a class that store all preferences. Can I send it to the Activity and back again? Or is the best way to store the preferences in a sqlite database and then use it for passing data?
I find it easier to segregate menus and such into separate activities (unless you are using dialogs etc..) As far as storing data you can do it a number of ways:
Database
StoredPreferences
Intent extras with putExtra/Bundle
Creating an application subclass and storing preferences there
Each have their merit. 4 is pretty easy as you just have to state the application class name in your manifest then call: MyAppClass app = (MyAppClass)getApplicationContext(); and you can then use any variables in MyAppClass via app. 2 is also straightforward.
You already pointed out the main difference: history management.
You can pass data to Activity via Intents putExtra()/getExtra():
Create an Intend and add custom data via Intent.putExtra(..)
Start the new Activity: startActivityForResult(intent).
Inside new Activity you can get extra data with intent.getXyzExtra() (where xyz is type).
When new Activity is done just call setResult(int, resultIntent). Again you can add extra data as described in 1.
Call finish() to end the activity.
In original Activity method onActivityResult will be called. Again extract data from Intent as described in 3.