What is the best way to share variables between 3 Activity’s?

Asked

Viewed 623 times

1

I have the variables:

photo_path
audio_path
commentary

And I would like Textoactivity, Send Alertactivity and Audioactivity activities to share these variables and keep the same value regardless of how many screen changes I make.

I tried to do this using Intent and at the time of changing screen passed the variables via putExtra(), but it is very difficult to give maintenance.

What are the other ways?

  • Edge, besides passing data through Intent, you can use SharedPreferences if they are configuration data. Cicero’s Idea is very good, but I recommend using instance variables and creating a Singleton (disengaging in the onDestroy) to avoid Memory Leaks.

3 answers

4


You can create a session:

public class App extends Application{


   public static String photo_path;
   public static String audio_path;
   public static String commentary;

}

And access in any of the acitvitys.

Put this on Androidmanifest.xml:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        **android:name=".App"**
        android:theme="@style/AppTheme" >

You can also save in preferences file the value of variables when using get and set.

1

One practice I use a lot in my projects is to create classes Uteis to centralize all the result_code and messages between Activity

An example

public class Prefs {
    public static final String PHOTO_PATH = "photo_path";
    public static final String AUDIO_PATH = "audio_path";
    public static final String COMMENTARY = "commentary";
}

Now sending to your Activity via Intent and receiving in another Activity:

Intent intent = new Intent(this, SuaActivity.class);
intent.putExtra(Prefs.PHOTO_PATH , photo_path);
intent.putExtra(Prefs.AUDIO_PATH , audio_path);
intent.putExtra(Prefs.COMMENTARY , commentary);
startActivity(intent);
...
if (getIntent().getExtras() != null) {
    String photo_path= getIntent().getExtras().getString(Prefs.PHOTO_PATH, "");
    String audio_path = getIntent().getExtras().getString(Prefs.AUDIO_PATH, "");  
    String commentary = getIntent().getExtras().getString(Prefs.COMMENTARY, "");  
}

You can do this too to centralize the reques_code in the startActivityForResult, for example:

public class RequestCode{
    public static final int UMA_ACAO = 0;
    public static final int OUTRA_ACAO = 1;
    public static final int MAIS_UMA_ACAO = 2;
}

And in his Activity

Intent intent = new Intent(this, SuaActivity.class);
intent.putExtra(Prefs.PHOTO_PATH , photo_path);
intent.putExtra(Prefs.AUDIO_PATH , audio_path);
intent.putExtra(Prefs.COMMENTARY , commentary);
startActivityForResult(intent, RequestCode.UMA_ACAO );    
    ...      
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == RequestCode.UMA_ACAO)
         //fazer algo
    else if (requestCode == RequestCode.OUTRA_ACAO)
         //fazer algo
    else if (requestCode == RequestCode.MAIS_UMA_ACAO)
         //fazer algo

    super.onActivityResult(requestCode, resultCode, data);
}

1

You can use the method mentioned above by @Cicero Moura to create an application of your own, or you can create objects using the Singleton standard, or join the two solutions that I consider the best option, because you will standardize and better organize your application, this will make your code easier to maintain.

public class Singleton {

    private static Singleton uniqueInstance;

    private Singleton() {
    }

    public static synchronized Singleton getInstance() {
        if (uniqueInstance == null)
            uniqueInstance = new Singleton();
        return uniqueInstance;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.