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);
}
Edge, besides passing data through
Intent
, you can useSharedPreferences
if they are configuration data. Cicero’s Idea is very good, but I recommend using instance variables and creating aSingleton
(disengaging in theonDestroy
) to avoid Memory Leaks.– Wakim