How to transfer data between "activities" of Android applications?

Keywords: Android Session JQuery Steam

I have a situation where after I log in through the login page, there will be an exit button on each activity.

Click sign out and I will pass the session id of the logged in user to exit. Who can tell me how to make session id available to all activities?

Any alternative to this

#1 building

The most convenient way to pass data between activities is to pass intent. In the first activity you want to send data, you should add code,

String str = "My Data"; //Data you want to send
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("name",str); //Here you will add the data into intent to pass bw activites
v.getContext().startActivity(intent);

You should also import

import android.content.Intent;

Then, in the next activity (second activity), you should use the following code to retrieve data from the intent.

String name = this.getIntent().getStringExtra("name");

#2 building

Another method is to use the public static fields that store data, that is:

public class MyActivity extends Activity {

  public static String SharedString;
  public static SomeObject SharedObject;

//...

#3 building

Data transfer between activities is mainly through intention objects.

First, you must attach the data to the intent object using the Bundle class. Then use the startActivity() or startActivityForResult() method to call the activity.

You can use the blog post "will Data transfer to Activity Find out more about it.

#4 building

I recently released Vapor API , this is a jQuery style Android framework, which makes all kinds of tasks like this easier. As mentioned earlier, SharedPreferences is one way you can do this.

VaporSharedPreferences Implemented as Singleton, it's an option, and in the Vapor API, it has overloaded. put(...) methods, so you don't have to worry explicitly about the type of data to submit - as long as it's supported. It's also fluent, so you can link to:

$.prefs(...).put("val1", 123).put("val2", "Hello World!").put("something", 3.34);

It also has the option to automatically save changes and unify the read and write processes in the background, so you don't need to explicitly retrieve the Editor as in standard Android.

Or, you can use Intent. In the steam API, you can also use methods on linkable overloads. put(...) VaporIntent :

$.Intent().put("data", "myData").put("more", 568)...

As described in the other answers, pass it on as extra content. You can retrieve other content from the Activity, in addition, if you are using VaporActivity This is done automatically, so you can use:

this.extras()

To retrieve them at the other end of the Activity, switch to.

Hope some people are interested:)

#5 building

You only need to send additional content when you send your intention.

Like this:

Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Variable name", "Value you want to pass");
startActivity(intent);

Now, on the OnCreate method of SecondActivity, you can get other functions like this.

If you send a value of long:

long value = getIntent().getLongExtra("Variable name which you sent as an extra", defaultValue(you can give it anything));

If you send a value of String:

String value = getIntent().getStringExtra("Variable name which you sent as an extra");

If the value you send is Boolean:

Boolean value = getIntent().getBooleanExtra("Variable name which you sent as an extra", defaultValue);

Posted by Roberto on Tue, 10 Dec 2019 14:36:09 -0800