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

1

I have the variables:

photo_path
audio_path
commentary

And I would like the TextActivity, SendAlertaActivity, and AudioActivity activities to share these variables and they would keep the same value regardless of how many screen changes I made.

I tried doing this using Intent and at the time of changing the screen I would pass variables via putExtra (), but it is very difficult to maintain.

What other ways?

    
asked by anonymous 10.10.2014 / 18:05

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 in 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 the value of the variables in preferences when using get and set.

    
10.10.2014 / 18:57
1

You can use the method mentioned above by @Ceroero Moura to create an Application itself, or you can create objects using the singleton pattern, or join the two solutions that I consider the best option, because you will standardize and better organize your application, so your code would be 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;
    }
}
    
10.10.2014 / 20:49
1

One practice that I use a lot in my projects is to create Uteis classes to center all 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 try and getting 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 also do this to center reques_code on 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 on your 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);
}
    
12.10.2014 / 03:43