public void onClick(View v) {
String value="Pass this";
Intent i = new Intent(MainActivity.this,LoadActivity.class);
i.putExtra("KEY",value);
startActivity(i);
I have been using this way of sending info to new activity and wanted to know if I can use this same way but not go to new activity just save the String in case I wanted to save multiple things in one activity then go to the next.
Once any activity is closed the data can only come back in one of the 2 ways.
If you pass a Bundle to the onSaveInstanceState() method or if you use SharedPreferences.
The onSaveInstanceState() method is usually used to store information while the device configuration changes, for instance: the orientation of the device.
Use SharedPreferences if you want data to be persistent across the entire app. That data can then be accessed from whatever activity you're in.
This Guide from Google explains SharedPreferences in details.It's quite easy to implement too compared to onSaveInstanceState() because you don't have to worry about the activity lifecycle.
Related
In the Intent used to trigger an android.intent.action.CREATE_DOCUMENT action, I add a custom extra with myIntent.putExtra("myPackage.MY_EXTRA","toto").
In the onActivityResult function, when I try to retrieve this extra with intent.getStringExtra("myPackage.MY_EXTRA"), I get a null String (intent is the Intent received as a parameter in the onActivityResult function).
Any idea on how I could solve my problem?
What you want is not likely to be available for any startActivityForResult() call. Putting an extra on an Intent sends that extra to the other activity. There is no requirement for that extra to be returned to you, and few (if any) activities will do that.
So, hold onto the data yourself, such as in a field of your activity.
I send an intent with information to start an activity. Now, instead of retrieving the info in the onCreate method, can I retrieve it after that moment? For example, when I click a button in the new activity?
Yes I think you can if you write it this way:
YourActivity.this.getIntent().getExtras();
getIntent().getExtras() yields the data in your current Activity.
If you want to pass this data to a new Activity you have to read them and write into a new Intent.
If the first activity is made as singleton then you can get the Intent data from the only Activity's instance.
Or you can make another singleton class just to hold that data for you.
I am trying to send the string from one Activity to Another WITHOUT changing the CURRENT ACTIVITY. This my code I used:
Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("getFollowerNumberData", txt);
startActivity(intent);
Using this code bring me to the other activity or I do not want that. I just want to send this string without changing activity. I tried this one
Intent intent = new Intent();
But it is crashing my app. First of all it is possible to do so? If yes, how can I achieve that?
Edit: To be more clear, it works like follower and Following features in other apps or games. When you follow someone the number of the person you just followed goes up as well your Following number. Since my SignInPlayerProfile.Class(where the Following should go up to one too) is in another activity I was trying to get this information intent.putExtra("getFollowerNumberData, txt); from Main Activity and display that in the SignInPlayerPlayer by using text.setText(). But the Problem is by using intent = new Intent(this, AnotherActivity.class); it brings me to SignInPlayerProfile Activity which I do not like since
I am trying to send the string from one Activity to Another WITHOUT changing the CURRENT ACTIVITY(or simpler words WIHTOUT Going to the Other Activity).
Thanks for Help.
When an activity getting closed, according to it's life-cycle, it will be destroyed and no longer exists, when you start an activity it will be created and then it can get your data, so you can't send data to an destroyed activity which no longer exists
You can use Static Variables to communicate between activities (Classes), you can change value of a Static-Variable of an activity from another activity, but it's not a good option for data you need to be alive because Static Variables lives on Heap Memory and Heap will be freed if Android OS needs more Memory
Another way is to create a Message Handler in your first activity as a Static Variable and then send a Message to the Handler from second activity, see this example :
http://stacktips.com/tutorials/android/android-handler-example
I suggest you using SharedPreferences for saving your data in first activity and load from it on second activity
EDIT :
According to your edit, the "Number" you want to use in another Activity as "Following" or "Followers" is just needed when the second activity is visible, you should use sharedpreferences to save the "Number" and load from it when you need it. For example before text.settext() method you can load the number from sharedpreferences and then pass it to text.settext()
You should not save your data on the variables or classes and should save them on a file like a Database or SharedPreferences then you can load them every time you want
Furthermore you can search about Activity life-cycle and see how to use life-cycle events like OnStart to load your data
Firstly please consider startActivityForResult() ,we can send information from one activity to another and vice-versa. As mentioned consider using shared_preferences for local in memory storage. To truly accomplish this feat in a elegant way though, do consider using obervables(rxandroid). You publish observations in one activity or fragment ,
then subscribe in another activity or fragment. I did not mentioned event bus nor otto since rxjava/rxandroid surpasses it. They act a promise context management system. Also because observable in process dependent consider using broadcast receivers, to broadcast events through out your application and external if so desired.
What is the easiest way to send an ArrayList<float[]> between activities?
Is it possible to send using SharedPreferences or putExtra?
I've only seen examples of sending ArrayList<String> or ArrayList<Int>, and those options are built in through .putStringArrayList etc.
The best way would be to add it as an extra to the Intent's Bundle. This is because Intent extras were created specifically to pass arguments between Activities.
Put Extra
ArrayList<float[]> list = new ArrayList<>();
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("arg_key", list);
Get Extra
ArrayList<float[]> list = ( ArrayList<float[]>) getIntent().getSerializableExtra("arg_key");
That said, if the data is to be persisted in a Database anyways, you would simply retrieve it from the database. I would not use SharedPreferences for this as it is intended for storing flags, tokens, app settings, etc.
UPDATE
If you want to pass an argument that is not supported by an Intent's extras and is not natively Serializable, have a look at Parcelable. You can implement Parcelable in any of your POJOs to allow them to be added to a Bundle. Parcelable is also faster than Serializable.
I have an application Activity that in onCreate loads an XML file from a service using an AsyncTask. The XML is parsed into an ArrayList. When I switch to a different activity and then back to the main activity, I want to be able to recognize that that XML file was already loaded and use the populated ArrayList.
What is the best way to persist that ArrayList?
onSaveInstanceState only seems to support primitives and I've been unable to set up a case where onRetainNonConfigurationInstance actually gets called. So in onCreate, the XML data is loaded from the server ever time I switch to that Activity. I have made the models that are in the ArrayList implement Parcelable, so could use that in some way?
What is the best way to persist that ArrayList?
I don't see where your problem has anything to do with multiple activities. What happens if the user presses HOME (gasp!), for example? Your app will eventually be closed. Do you want to reload the data from the server? If the answer is "yes", then you don't need to "persist" anything, and onSaveInstanceState() may suffice (see below). If the answer is "no", then you need to rethink your approach to your data model, so you arrange to keep the data in a database, synchronizing with your Web service periodically, and probably dumping the ArrayList and replacing it with a Cursor.
onSaveInstanceState only seems to support primitives
If the answer to my HOME question is "yes", then you can just hold onto the data in a data member of your activity, and, if it is modestly sized, also stash it in the Bundle in onSaveInstanceState(). A Bundle can hold an ArrayList of Parcelable. However, if the data set is large (say, 100KB or more), you probably don't want to go this route and should consider the "no" path I described above.
I've been unable to set up a case where onRetainNonConfigurationInstance actually gets called.
Rotate the screen. There are other scenarios, but orientation changes are the easiest ones to trigger it.
However, it has nothing to do with your problem.
"onSaveInstanceState only seems to support primitives"
onSaveInstanceState supports objects, as long as they are declared serializable.
// ON_SAVE_INSTANCE_STATE
// save instance data (5) on soft kill such as user changing phone orientation
protected void onSaveInstanceState(Bundle outState){
password= editTextPassword.getText().toString();
try {
ConfuseTextStateBuilder b= ConfuseTextState.getBuilder();
b.setIsShowCharCount(isShowCharCount);
b.setTimeExpire(timeExpire);
b.setTimeoutType(timeoutType);
b.setIsValidKey(isValidKey);
b.setPassword(password);
state= b.build(); // may throw
}
catch(InvalidParameterException e){
Log.d(TAG,"FailedToSaveState",e); // will be stripped out of runtime
}
outState.putSerializable("jalcomputing.confusetext.ConfuseTextState", state); // save non view state
super.onSaveInstanceState(outState); // save view state
//Log.d(TAG,"onSaveInstance");
}