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");
}
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.
This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 4 years ago.
I am very new to Android Studio, and have found myself stuck in this concept. I am attempting to pass Price + Name data to a Cart activity upon a button press (Add to Cart).
After attempting intent methods, it seems that after pressing "Add to Cart", the cart is opened with the data, but the data is not saved in the new activity for more additions.
Right now I have the following:
Button button = (Button) findViewById(R.id.addcart);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//How to pass information here
}
});
I am hoping to pass textView6 and textView7 to the cart activity. If possible, I would be interested in passing the image as well! Any start on this would be appreciated. Thank you!
To pass data trhoug activities you can use the same intent you use to open the new activity. You can set extras like this:
Intent i = new Intent(context, CartActivity.class);
i.putExtra("price", textView6.getText().toString());
i.putExtra("name", textView7.getText().toString());
startActivity(i);
And then in onCreate() of the just created activity you can retrieve this data getting the intent used to open this activity and getting its extras:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent data = getIntent();
String price = data.getStringExtra("price");
String name = data.getStringExtra("name");
}
Hope this help.
When you create your Intent you have to do :
Intent i = new Intent(this, ToClass.class);
i.putExtra("someName", findViewById(R.id.textView6 ).getText().toString());
i.putExtra("someName2", findViewById(R.id.textView7 ).getText().toString());
startActivity(i);
And then in your second Activity, use :
Intent intent = getIntent();
String someName= intent.getStringExtra("someName");
String someName2= intent.getStringExtra("someName2");
You say your using an intent, but what are you doing with the values? Are you saving them somewhere in the CartActivity?
For the image just pass a reference or name of the image. No need to pass the whole PNG... and again use an intent putExtra() call.
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?
I'm a PHP Web Developer. When I create a view article page I was passing the id in GET something like article.php?id=10
But now in android if I have an activity which views the article, how to pass to it the article ID to select it's data from the database?
What is my best option?
I assume you have 2 Activities. The first one has a list of articles. When you click on an article you start your second activity.
In you onClick() method:
Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("id", 10);
startActivity(i);
In you second activity:
int id = getIntent().getIntExtra("id", 0); //0 is a default value
You have to pass the article id in the intent when calling the ArticleActivity. Here is how to do it:
Intent intent = new Intent(this, ArticleActivity.class);
intent.putExtra("articleId", articleId);
startActivity(intent);
In this code, this is the context of your current activity; articleId is your articleId you want to pass. Then, when you want to retrieve it in the ArticleActivity, you do this in onCreate of the activity:
Bundle extras = getIntent().getExtras();
int articleId = extras.getInt("articleId");
Thats it! Hope that helped.