简体   繁体   中英

Android send extra Data through multiple Activities

I have a chain of intents

1) Select File

2) Overlay some stuff

3) Save on Disk

4) Upload to Server

This means that i am using those commands 4 times:

video_id = extras.getString("video_id");
i.putExtra("video_id", video_id);'

Is there another neat way of doing that, without having to write anything on disc or db?

You can use Singleton Class ie Application in android. It maintains global application state.

public class GlobalClass extends Application{
    private String name;

    public String getName() {

        return name;
    }
}

Assign GlobalClass.java in application tag in application Manifest.xml android:name="com.androidexample.globalvariable.GlobalClass"

After Assigning you can set or access this variable from any activity using following code.

    final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
    //Set name in global/application context
    globalVariable.setName("Android Example context variable");

   //get name frim global/application context from any other activity.
   String name = globalVariable.getName();

Use SharedPreference. Please check http://developer.android.com/training/basics/data-storage/shared-preferences.html SharedPreference will not delete automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM