Retrieving data between more than one Activities [duplicate] - android

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 9 years ago.
I am new to android application I have a task is that i should pass data from activity A->Activity B and should pass data between Activity B-----------> Activity C Same way I should get data from Activity C-------> Activity A please tell the scenario or send an example for me

Try two options
1). Intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("KEY", "VALUE");
startActivity(intent);
2). Shared Preference if the data is shared between all activities.

You can pass data from A to B by using intent:
Intent intent = new Intent(this, Activity.class);
intent.putExtra("name", "value");
startActivity(intent);
You can return back value using onActivtyResult() or by storing it in
shared preferences
If you are using onActivtyResult() method you need to start activty for result using : startActivityForResult() method
You can check this for sending data back
send data back to activity

Intent is what you needed:
Intent mIntent = new Intent(ActivityA.this, ActivityB.class);
mIntent.putExtra("key", "value");
startActivity(mIntent);
The above code starts ActivityB and sends the data along with it, to ActivityB (from mIntent.putStringExtra("key", "value");)
You can also see the different prototypes of this from here.
On the next activity, ActivityB, you can get the data sent by code below (write it inside onCreate() in ActivityB):
Intent mIntent = getIntent();
String data_recieved = mIntent.getStringExtra("key"); \\ here key is same as mentioned in previous activity from where you reached here.

Related

How to clear previously opened activity in Android

Intent intent = new Intent(MainActivity.this, Payment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Opening coupon activity to apply coupon:
Intent intent = new Intent(Payment.this, Coupon.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
From coupon activity opening payment activity with updated data
Intent intent = new Intent(Coupon.this, Payment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
The problem is, when I click on the back button on payment activity after applying the coupon, it opens the previously opened payment activity again (not the main activity).
onBackPressed() I don't want to do it static (like Intent intent = new Intent(Payment.this, MainActivity.class); and also don't want to use finish(); in payment activity.
Please help.
You can use FLAG_ACTIVITY_NO_HISTORY when launching the Payment activity.
See: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_NO_HISTORY
This is a Simple Use case of startActivityForResult don't make it complicated with Intent Flags and launch modes.
Whenever you Open your Activity Coupon just use startActivityForResult and upon applying coupon send the result back to previous Activity (success/failure) or whatever result you want . Then you can change the UI as per the result you got in onActivityResult() .
You can follow How to manage startActivityForResult on Android or the latest way from the doc Getting a result from an activity .

how to pass user input from activityOne to activityThree, activityFour, activityFive in Android?

I'm learning Android and trying to pass user input from my first activity to several activities but not the second activity. actually I'm going to get more data in my second activity and later use that also in my later activities but not in order. How can I do that?
you make the variable as static or you cab send data as extras.
Set data in intent:
Intent intent = new Intent(FirstActivity.this,SecondActiviy.class);
intent.putExtra("key",value);
startActivity(intent);
Fetch data from intent:
String data = getIntent().getStringExtra("key");

Sending data to activity

So,I am trying to find out a way to send data to an activity WITHOUT starting it.
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(MESSAGE_KEY, message);
startActivity(intent);
Is there a way to do it without the(?):
startActivity(intent);
Well if you don't want to start an activity and pass the data then why to worry so much? Make the variable public and from the other activity class call the previous activity as follows and access that particular variable
((FirstActivity)getActivity()).theVariable

How to send data from one activity to another by using getIntent() instead of usinf new Intent();

usually we send data from one activity to another by using:-
Intent i=new Intent("<action name>");
i.putExtras("name",data);
startActivity(i);
My question is can we send data from one activity to another by using:-
Intent i=this.getIntent();
i.putExtras("name",data);
setResult(Activity.RESULT_OK,i);
finish();
If yes please explain the concept.
Also,these two classes are in the different projects in Eclipse.My another question is, is it possible to send data through intent to another activity situated in another project??
So you're starting from Activity A and going to Activity B using startActivityForResult()
Now we're in Activity B and want to go back to Activity A:
Intent i = new Intent();
if(getIntent().getExtras() != null) i.putExtras(getIntent().getExtras());
setResult(Activity.RESULT_OK, i);
finish();
Doing something like that will allow you to pass back the extras from the calling Intent (if they exist), which would then be reachable from Activity A's onActivityResult() method.
Yes. You can do so, when you call another intent for a result. ie startActivityForResult(Intent,Request_Code);

use the bundle in more activities?

I have main activity and two another activities(second and third). I create a bundle in main activity and intent like thisIntent intent = new Intent(this,second.class); and I want to use get extras in third activity. like bundle comes = getIntent().getExtras();. is it possible or why not?
and I want to use get extras in third activity. like bundle comes =
getIntent().getExtras();. is it possible or why not?
You are able to getExtras() only from Intent that will start your third Activity. So in other words if your MainActivity starts third Activity you are able to retrieve data in third Activity if you'll append them into intent that starts third Activity:
// this is calling from MainActivity
Intent intent = new Intent(this, Third.class);
intent.putExtras(bundle);
If MainActivity doesn't start third Activity, you are not able to do it. But you're able retrieve data in third Activity from your second Activity (again here you have to append data to Intent):
// this is calling from SecondActivity
Intent intent = new Intent(this, Third.class);
intent.putExtras(bundle);
You can also use the "Shared Preferences" to store and access some data, anytime you wish.
http://developer.android.com/guide/topics/data/data-storage.html

Categories

Resources