How do I pass data between Activities in Android a

2020-01-22 10:15发布

I have a scenario where, after logging in through a login page, there will be a sign-out button on each activity.

On clicking sign-out, I will be passing the session id of the signed in user to sign-out. Can anyone guide me on how to keep session id available to all activities?

Any alternative to this case

30条回答
ゆ 、 Hurt°
2楼-- · 2020-01-22 10:36

The most convenient way to pass data between activities is by passing intents. In the first activity from where 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 Acitvity(SecondActivity), you should retrieve the data from the intent using the following code.

String name = this.getIntent().getStringExtra("name");
查看更多
聊天终结者
3楼-- · 2020-01-22 10:37

Another way is to use a public static field in which you store data, i.e.:

public class MyActivity extends Activity {

  public static String SharedString;
  public static SomeObject SharedObject;

//...
查看更多
一夜七次
4楼-- · 2020-01-22 10:38

Use a global class:

public class GlobalClass extends Application
{
    private float vitamin_a;


    public float getVitaminA() {
        return vitamin_a;
    }

    public void setVitaminA(float vitamin_a) {
        this.vitamin_a = vitamin_a;
    }
}

You can call the setters and the getters of this class from all other classes. Do do that, you need to make a GlobalClass-Object in every Actitity:

GlobalClass gc = (GlobalClass) getApplication();

Then you can call for example:

gc.getVitaminA()
查看更多
别忘想泡老子
5楼-- · 2020-01-22 10:39

Passing Intent extras is a good approach as Erich noted.

The Application object is another way though, and it is sometimes easier when dealing with the same state across multiple activities (as opposed to having to get/put it everywhere), or objects more complex than primitives and Strings.

You can extend Application, and then set/get whatever you want there and access it from any Activity (in the same application) with getApplication().

Also keep in mind that other approaches you might see, like statics, can be problematic because they can lead to memory leaks. Application helps solve this too.

查看更多
做个烂人
6楼-- · 2020-01-22 10:40

Try to do the following:

Create a simple "helper" class (factory for your Intents), like this:

import android.content.Intent;

public class IntentHelper {
    public static final Intent createYourSpecialIntent(Intent src) {
          return new Intent("YourSpecialIntent").addCategory("YourSpecialCategory").putExtras(src);
    }
}

This will be the factory for all your Intents. Everytime you need a new Intent, create a static factory method in IntentHelper. To create a new Intent you should just say it like this:

IntentHelper.createYourSpecialIntent(getIntent());

In your activity. When you want to "save" some data in a "session" just use the following:

IntentHelper.createYourSpecialIntent(getIntent()).putExtra("YOUR_FIELD_NAME", fieldValueToSave);

And send this Intent. In the target Activity your field will be available as:

getIntent().getStringExtra("YOUR_FIELD_NAME");

So now we can use Intent like same old session (like in servlets or JSP).

查看更多
看我几分像从前
7楼-- · 2020-01-22 10:42
/*
 * If you are from transferring data from one class that doesn't
 * extend Activity, then you need to do something like this.
 */ 

public class abc {
    Context context;

    public abc(Context context) {
        this.context = context;
    }

    public void something() {
        context.startactivity(new Intent(context, anyone.class).putextra("key", value));
    }
}
查看更多
登录 后发表回答