so I have a doubt, I have 3 activities, in the first one I send some data to the second one, and in the second one I send some data to the third one and finish the second one, is there a way I can finish the third activity and send some data to the first activity from the third?
This is what I'm doing in the first activity.
mStartGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(view.getContext(), GameInProgress.class);
i.putExtra("questionObject", questions);
startActivityForResult(i,DETAIL_REQUEST);
}
});
And in the second one after i get the extra I do this to send data to the third activity:
Intent i = new Intent(view.getContext(), StatisticsActivity.class);
i.putExtra("questionObject", questionInfo);
startActivity(i);
finish();
Now in the third I get the extras and then I:
Intent returnIntent = new Intent();
isCorrect = true;
returnIntent.putExtra("questionCorrection", isCorrect);
setResult(RESULT_OK, returnIntent);
finish();
I though this would work because when I finish the third activity the onActivityResult in the first activity method is working but when I try to access the Extras I get a nullpointerexception, so I don't know, thank you.
Sorry dude but I doubt your procedure of sending data from 3rd to 1st Activity.If data is simple string or integer. You can use Shared Preference Follow link
How to use Shared preference
Edit Shared Preference
Why Don't you also finish First activity and start new Instance of same activity through intent?
Related
I have 3 activities.
The first activity is a map (using Google Map API). In this activity, I also have a button that will direct to second activity.
In Second activity, it's just for additional information but there is a button. When this button is clicked, it will be directed to third activity.
In third activity, there's only a button that will bring a result to first activity (maps).
This is how I assigned my first activity's button:
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
And I also used ActivityResultLauncher in first activity like so:
ActivityResultLauncher<Intent> intentLaunch = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if(result.getResultCode() == Activity.RESULT_OK){
String data = result.getData().getStringExtra("SIMPAN");
Log.d(TAG, "berhasil");
}
}
);
In my third activity's button, I assigned it like this:
third_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("SIMPAN", "Simpan");
setResult(Activity.RESULT_OK, intent);
finish();
startActivity(new Intent(ThirdActivity.this, FirstActivity.class)); // When the button is clicked, it will get back to first activity and bringing the result
}
});
But, turns out the first activity didn't get the result. I assume that I missing this particular code after using ActivityResultLauncher:
Intent intent = new Intent(FirstActivity.this, ThirdActivity.class);
intentLaunch.launch(intent);
However, it makes the application starts from third activity instead of first activity. It does receive the result (the log was showing) but it's only one time. How can I resolve this?
(Sorry for my bad english)
This is an overkill.
Simply:
Intent intent = getIntent();
String simpan = intent.getStringExtra("SIMPAN");
in your onCreate class
registerForActivityResult is used when you get a result from outside the app, such as getting a picture from the camera.
I get input from user in first activity and send that to a second activity. From the second activity, using the user input, I fetch data from the db and want to send that data to a third activity. How do I give the intent from the second activity to the third activity?
Presumably you are starting each activity using an Intent in which case you could pass data as an extra.
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("user_input_key", "user_input");
startActivity(i);
In the second activity onCreate
Bundle extras = getIntent().getExtras();
if (extras != null) {
String userInput = extras.getString("user_input_key");
//Once activity starts use this string to get data from DB and put/get extra in the same way
}
You shouldn't be passing the intent between activities. Identify exactly what you want to pass between activities and put that data in the intent as an extra and retrieve that data in the new Activity onCreate.
So i got two activities, a main then a secondary, we'll call them activity_A (main) and activity B( the second ).
So my activity_A is getting some data, then sending it to activity_B with this code :
Intent i = new Intent(this,activity_B.class);
i.putExtra("Charge_Batterie", bat);
startActivity(i);
activity_B receive it correctly with this code :
String bat = getIntent().getStringExtra("Charge_Batterie");
My problem is that my activity_A is refreashing it's value "bat", so when i send it with intent it doesn't refreash in activity_B.
So i'm wondering, is my activity_A sleeping ? If yes how can i make it stay alive ?
I´m doing the same thing and it works for a long time - activity A runs a timer and launches activity B with extras.
When I want to refresh activity B with "new extras", I´m doing this:
// check if activity B is created and running:
if (intent == null) {
System.out.println("Creating Activity - Intent is null");
intent = new Intent(context, ActivityB.class);
intent.putExtras(ImagedownloaderActivityBundle);
startActivity(intent);
}else {
System.out.println("Reloading Activity - Intent exists");
finish();
intent = new Intent(context, ActivityB.class);
intent.removeExtra("playlistsList");
intent.putExtras(ImagedownloaderActivityBundle);
startActivity(intent);
}
As far as I know, this isn´t the best way to achieve the background task - it should be running on a service, perhaps doing the same when one needs to refresh.
I have question about passing data between activities. Is possible from main activity open few next activities, and return result from last opened to root activity and how? I've added screenshot for better vision what I mean. Thanks for answers.
enter image description here
working with this question #Sahil Lombar
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("data", "data");
startActivity(i);
in other activity we can
//firt validate if value extra "data" it was sent
if(getIntent().hashExtra("data") {
String value = getIntent().getStringExtra("data");
Log.d("SecondActivity", value);
}
working with String, int, boolean, double, Byte and array of these
more.. https://developer.android.com/reference/android/content/Intent.html
Start the First Activity with Intent holding the result;
In Activity 3
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("data", "data");
startActivity(i);
finish();
In Activity 1 receive the result in public void onNewIntent or if you are starting a new activity in the onCreate method
public void onCreate(){
super.onCreate();
Intent i = getIntent();
String result = i.getStringExtra("data");
}
I want data to be synchronized between two Activities. I have TextView1 inside 1st Activity and TextView2 inside 2nd Activity. The 2nd Activity is started form the 1st. After that the data in TextView2 is changed. When I'll be back to the 1st Activity, the data in TextView1 has to be the same with TextView2's data. I've tried to use intents, but it's not possible to be as the 1st Activity is crashed because it's waiting for the data, I suppose.
The 1st Activity:
.....
level = getIntent().getExtras().getString("level");
score = getIntent().getExtras().getString("score");
.....
The 2nd Activity:
.....
Intent intent = new Intent(2nd_activity.this, 1st_activity.class);
intent.putExtra("level", Integer.toString(level));
intent.putExtra("score", Integer.toString(score));
.....
I guess you have figured it out why it doesn't work.
What do I need to do to solve this problem?
You can use startActivityForResults to open 2nd Activity, when 2nd activity is supposed to be closed then you call:
Intent returnIntent = new Intent();
returnIntent.putExtra("tv_text",tv.getText());
setResult(RESULT_OK,returnIntent);
and in activity 1, you will receive results in onActivityResult and update textview in activity 1 with Intent data, sample code is from:
How to manage `startActivityForResult` on Android?
You have to check that the call to getIntent() does not return null, as is the case when you first launch 1st Activity
Intent rcvdIntent = getIntent();
if (rcvdIntent != null) {
level = rcvdIntent.getExtras().getString("level");
score = rcvdIntent.getExtras().getString("score");
}