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
Related
I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();
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
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);
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.
I am implementing a simple app. I need to start an activity based on the state of the Activity. Lets take i am using a button to start the activity.
1. If the activity is not started, I need to start XYZ activity.
2. If the XYZ activity is on focus, then i need to close the activity on the button press.
3. If the XYZ activity is not in focus (like onPause) state then, I need to change the button state.
Can you please help me in the flags that i need to use for starting the intent.
Is it possible to get the state of activity before I start that activity?
Try this
Intent intent = new Intent(currentActivity.this, callingActivity.class);
startActivity(intent);
You can use intent like this to call an activity
The button press will need to be captured by each activity separately, so just code the different responses into each different activity.
First off create a MAIN.java activity that is going to house your other activities. Like others have said you're going to have to code the button captures yourself because that should be common sense if you're trying to deal with intents. When you get that together though, you can start a new activity through intent like so:
// allocate new intent, initialized to the activity you wish to launch
Intent i = new Intent(this, ActivityToBeLaunched.class);
// put information into intent
i.putExtras("KeyName", value); // where "KeyName" is simply a reference string
// and 'value' can be anything from boolean - string.
// launch activity and wait for response
startActivityForResult(i, REQUEST_CODE);
Then within your ActivityToBeLaunched.java class you'll have an oncreate that will pull information from the intent like such:
// get intent
Intent i = this.getIntent();
// get information from intent
booleanVariable = i.getExtras().getBoolean("KeyName");
When you're done with this activity simply use;
// create intent
Intent i = new Intent();
// put information into result to send back to parent
i.putExtras("KeyName", value);
// set the result to be returned
setResult(i, ResultCode);
// finish child, return to parent with results
finish();