I have a main_activity calling activity B. Activity B will update a few things on main_activity, while updating a few things on itself. A back button press on activity B will bring it back to main_activity. I managed to keep activity B's contents intact, but not the main_activity (being recreated). I referred to this SO question.
What I want is to keep both main_activity's and activity B's contents intact. How can I achieve that?
This is my code:
main_activity calls activity B on menu select:
Intent mIntent = new Intent(mainActivity.this, Activity_B.class);
//startActivityForResult(mIntent);
mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(mIntent);
activity-B back press:
#Override
public void onBackPressed() {
Intent mIntent = new Intent(Activity_B.this, mainActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
startActivity(mIntent);
}
You can save contents of MainActivity that you want to update in ActivityB using SharedPreferences and in MainActivity get the value from SharedPreferences and display.
//Save something in Activity B
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor e = settings.edit();
e.putString("variableName", "a string value");
e.commit();
//Retrieve in MainActivity
String mContent = settings.getString("variableName", "");
SharedPreferences will store the value even if you exit the app. You can also access the values stored in SharedPreferences from any other class.
U need to start your activity from main activity as below
Intent mIntent = new Intent(mainActivity.this, Activity_B.class);
startActivityForResult(mIntent,PICK_CONTACT_REQUEST(int));
**in activity B ( for set result to update your main activity by Intent ) **
#Override public void onBackPressed() {
Intent mIntent = new Intent ();
mIntent.putExtra("some_key",someData);
mIntent.putExtra("Some_mey2",someData2);
setResult(RESULT_OK,mIntent);
}
**handling result in your main_activity from activity b and update ui of main_activity ** `
#Override
protected void onActivityResult(int. requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) { // Make sure the request was successful
if (resultCode == RESULT_OK) {
someTextfield.settext(data.getStringExtra("Some_key");
}
}
}
Related
I am using 5 activities. from activity1 I move to activity2, from act2 to act3, from act3 to act4 and from act4 to act5.
activity 2 carries data to act3 and like this act5 receives data of act2, act3, act4 and then send all data to act 1.
My First activity
I{
Intent i= new Intent(firstactivity.this, secondactivity.class);
startActivityForResult(i, 10);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10) {
String a= data.getStringExtra("Value1");
String b= data.getStringExtra("Value2");
String c= data.getStringExtra("Value3");
String d= data.getStringExtra("Value4");
String showall = a+", "+b+", "+c+", "+d;
address.setText(showall);
}
My Second activity
Intent intent = new Intent(secondactivity.this, thirdactivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent.putExtra("Value1", firstvalue);
startActivity(intent);
My Fifth activity
Intent intent = new Intent(fourthactivity.this, fifthactivity.class);
intent.putExtra("Value1", geta);
intent.putExtra("Value2", getb);
intent.putExtra("Value3", getc);
intent.putExtra("Value4", getd);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
setResult(10);
finish();
Try below:
Intent intent = new Intent(fifthactivity.this, firstactivity.class);
intent.putExtra("Value1", geta);
intent.putExtra("Value2", getb);
intent.putExtra("Value3", getc);
intent.putExtra("Value4", getd);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent);
And then process your passed result in onCreate (in case activity got destroyed) or onNewIntent (if your activity is still running but you bring it to front and update it with new intent) of your firstactivity
//A simple approach to solve this issue is to use sharedpreferences
//store value from activity five
getsharedpreferences('temp','MODE_PRIVATE').edit().clear().putString('values','new value').apply();
//get values from shared preferences if its available
String value 5 = getsharedpreferences('temp','MODE_PRIVATE').getString('values','nil');
getsharedpreferences('temp','MODE_PRIVATE').edit().clear();
first activity
static ArrayList<String> arlist=new ArrayList<String>();
arlist.add("value");
arlist.add("value1");
arlist.add("value2");
In second Activity
ActivityOne.arlist
In my app, there are so many activities which are referred to as levels. And one activity is Reward activity. when i win level-1, reward activity opens. Now i want to replay the level-1. For this i have used getExtra(). My app crashes when i click the replay button.
Houselevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.HouseLevel2");
startActivity(intent);
}
if(activity.equals("level1")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Houselevel1");
startActivity(intent);
}
}
});
}
With the java code you've posted, in the Reward.java file, you're trying to create another Intent Object with the same name as the one declared in the scope right above it. Because of this, the build will never be successful.
Also, when you declare intents, you MUST pass on the activity_name.class file.
Something you can try:
1) HouseLevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
2) HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
3) Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.HouseLevel2.class);
startActivity(intent);
}
else if(activity.equals("level1")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Houselevel1.class);
startActivity(intent);
}
}
});
}
Also, if you're simply using the Reward.java file to get the previous intent's data, perform some calculation, and send some data back to the calling, or parent activity, then you can simply use the startActivityForResult() method, which takes care what what you're trying to do manually.
Here's a small article that might be able to help you with the problem
http://www.vogella.com/tutorials/AndroidIntent/article.html#retrieving-result-data-from-a-sub-activity
If all you want is go from Activity 1 or to 2 to a Reward activity grab something and send that something back to either activity.
What you do is startActivityForResult You pass an Id (constant number) do what you do on the Reward activty, pack what you need to return in a Bundle, and set ActivtyResult to OK and close your activity.
Your app will go back to the Activity1 or 2 whoever call it. On those activties you override the method onActivityResult There you check if the id on which the result is coming from is the Id you sent on the startActivityForResult and if the status is OK.
Then you have whatever was set on the Reward activity. The Reward activity don't need to know from where it came from if only will grab some data. So you can later have an Activity3 that calls the Reward activity and you do not need to modify the Reward activity.
It is explain here check the accepted answer.
How to manage `startActivityForResult` on Android?
I've 2 activities, and need the main Activity to send some data to the second Activity then the second activity analyze this data and send response back, so I've the below 2 codes:
Main Activity:
import static tk.zillion.mobile.SecondActivity.EXTRA_STUFF;
public class MainActivity extends Activity {
private static int PICK_CONTACT_REQUEST = 0;
private static final int SECOND_ACTIVITY_RESULT_CODE = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Start the SecondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, "my text");
startActivityForResult(intent, SECOND_ACTIVITY_RESULT_CODE);
finish();
}
// This method is called when the second activity finishes
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this, "I'm the Main activity", Toast.LENGTH_SHORT).show();
// check that it is the SecondActivity with an OK result
if (requestCode == SECOND_ACTIVITY_RESULT_CODE) {
if (resultCode == RESULT_OK) {
// get String data from Intent
String returnString = data.getStringExtra(EXTRA_STUFF);
// set text view with string
Toast.makeText(this, "I'm the Main activity", Toast.LENGTH_SHORT).show();
}
}
}
and the Second Activity is as below:
public class SecondActivity extends Activity {
static final String EXTRA_STUFF = "tk.zillion.mobile.EXTRA_STUFF";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
String s = getIntent().getStringExtra(Intent.EXTRA_TEXT);
Toast.makeText(this, "I'm the second activity " + s , Toast.LENGTH_SHORT).show();
Bundle basket =new Bundle();
basket.putString(EXTRA_STUFF, s);
intent.putExtras(basket);
setResult(RESULT_OK, intent);
finish();
}
}
The second activity is fired correctly, and receive the data from the main one, but once the data is sent back the onActivityResult is not fired!!
How can I solve it? thanks
Remove the finish() call that comes after startActivityForResult(). You prevent the oncoming activity to give a result back, because you remove your MainActivity with finish() from the back task.
Dont call finish() after startActivityForResult otherwise that instance of MainActivity that started the second one will be destroyed
I'm wondering, is it possible to send information to the activity that I return to after calling finish()?
For example, I have an Activity SendMessageActivity.class which allows the user to post a message to their feed. Once that message has been saved to the server, I call finish(). Should I instead just start my MainActivity.class with a new Intent? Or is it better for life cycle development to just finish SendMessageActivity.class?
I don't see the point of starting a new activity since closing the current one will always bring you back to MainActivity.class. How can I just send a String extra after finishing the current Activity?
Use onActivityResult.
This might help you to understand onActivityResult.
By using startActivityForResult(Intent intent, int requestCode) you can start another Activity and then receive a result from that Activity in the onActivityResult() method.So onActivityResult() is from where you start the another Activity.
onActivityResult(int requestCode, int resultCode, Intent data) check the params here. request code is there to filter from where you got the result. so you can identify different data using their requestCodes!
Example
public class MainActivity extends Activity {
// Use a unique request code for each use case
private static final int REQUEST_CODE_EXAMPLE = 0x9988;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an Intent to start AnotherActivity
final Intent intent = new Intent(this, AnotherActivity.class);
// Start AnotherActivity with the request code
startActivityForResult(intent, REQUEST_CODE_EXAMPLE);
}
//-------- When a result is returned from another Activity onActivityResult is called.--------- //
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// First we need to check if the requestCode matches the one we used.
if(requestCode == REQUEST_CODE_EXAMPLE) {
// The resultCode is set by the AnotherActivity
// By convention RESULT_OK means that what ever
// AnotherActivity did was successful
if(resultCode == Activity.RESULT_OK) {
// Get the result from the returned Intent
final String result = data.getStringExtra(AnotherActivity.EXTRA_DATA);
// Use the data - in this case, display it in a Toast.
Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show();
} else {
// AnotherActivity was not successful. No data to retrieve.
}
}
}
}
AnotherActivity <- This the the one we use to send data to MainActivity
public class AnotherActivity extends Activity {
// Constant used to identify data sent between Activities.
public static final String EXTRA_DATA = "EXTRA_DATA";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
final View button = findViewById(R.id.button);
// When this button is clicked we want to return a result
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Create a new Intent as container for the result
final Intent data = new Intent();
// Add the required data to be returned to the MainActivity
data.putExtra(EXTRA_DATA, "Some interesting data!");
// Set the resultCode to Activity.RESULT_OK to
// indicate a success and attach the Intent
// which contains our result data
setResult(Activity.RESULT_OK, data);
// With finish() we close the AnotherActivity to
// return to MainActivity
finish();
}
});
}
#Override
public void onBackPressed() {
// When the user hits the back button set the resultCode
// to Activity.RESULT_CANCELED to indicate a failure
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
}
Note : Now check in MainActivity you startActivityForResult there you specify a REQUEST_CODE. Let's say you want to call three different Activities to get results.. so there are three startActivityForResult calls with three different REQUEST_CODE's. REQUEST_CODE is nothing but a unique key you specify in your activity to uniquely identify your startActivityForResult calls.
Once you receive data from those Activities you can check what is the REQUEST_CODE, then you know ah ha this result is from this Activity.
It's like you send mails to your lovers with a colorful covers and ask them to reply in the same covers. Then if you get a letter back from them, you know who sent that one for you. awww ;)
You can set result of an activity, which allow you to data into an initent.
In your first activity, call the new one with startActivityForResult() and retrieve data in method onActivityResult. Everything is in documentation.
try this:
in First Activity:
Intent first = new Intent(ActivityA,this, ActivityB.class);
startActivityForResult(first, 1);
Now in Second activity: set Result during finish()
Intent intent = new Intent();
intent.putExtra("result",result); //pass intent extra here
setResult(RESULT_OK,intent);
finish();
First activity Catch the result;
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 1
if(requestCode==1)
{
String message=data.getStringExtra("result");
//get the result
}
}
If you call finish() to avoid that the user go back to SendMessageActivity.class, you can set this flags to your intent:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
This will open the MainActivity and remove the SendMessageActivity from the activities stack.
I'm writing an app that has multiple activities. Activity A calls Activity B, not expecting a result. Then if a button is pressed B startsActivityForResult with Activity C. When Activity C is done, it makes an intent with all of the extras it needs and finishes. The problem is that when it calls this.finish() or just finish(), it brings me all the way back out to Activity A. onActivityResult in Activity B is not called. What is wrong?
Activity A: Starts Activity B
Intent in = new Intent(ccstart.this,mainmenu.class);
in.putExtra("uid",loginresponse);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("usr",text_user.getText().toString());
// Commit the edits!
editor.commit();
startActivity(in);
Activity B: Starts Activity C for result
Intent intent = new Intent(mainmenu.this,filebrowser.class);
startActivityForResult(intent,0);
Activity C: Return statement
Intent result = new Intent();
result.putExtra("fname", file.getAbsolutePath());
this.setResult(Activity.RESULT_OK, result);
finish();
Activity B: Upon the result of activity c...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// If the request went well (OK) and the request was PICK_CONTACT_REQUEST
if (resultCode == Activity.RESULT_OK && requestCode==0) { //upload a file
final String fname = data.getExtras().getString("fname");
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); //Load settings
final String uid = settings.getString("uid", "");
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
doFileUpload(fname, uid);
}
}).start();
}
}
What is the issue with that? It happens with an activity that doesn't return a result as well, so its not just this one.
Thanks!
You need to explicit close your activity when you start the next one, if not, it stays in the "stack of activities" that you can access with the back button or when the next activity closes.
You need to call finish on activity A after you started activity B