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
Related
I want to move data from an ArrayList in a second activity to my main activity.
This is my main.java file
super.onActivityResult(requestCode, resultcode, data);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.callingOrder);
ArrayList<String> numbersList = (ArrayList<String>) getIntent().getSerializableExtra("key");
textView.setText(String.valueOf(numbersList));```
public void onBackPressed(){
Intent intent = new Intent();
intent.putExtra("key", sandwichOrder);
setResult(RESULT_OK, intent);
finish();
the second.java file above ^
I only get a null message on my xml file. Point is to move the data when I press the back arrow button.
In this line, you're looking in the wrong Intent:
ArrayList<String> numbersList = (ArrayList<String>) getIntent().getSerializableExtra("key");
getIntent() will return the Intent that was used to start your activity. Anything you pass "back" with setResult() will be in the data parameter to onActivityResult(). So write this instead:
ArrayList<String> numbersList = (ArrayList<String>) data.getSerializableExtra("key");
In first activity, there is empty ListView and Button.
When I press button, it starts second activity that has ListView of categories.
After I click into one of listElements it will start third activity that has ListView with elements that are belong to my chosen category.
When I choose element of third ListView it must send me back to first activity, where my chosen element is added to my empty ListView
Use Intent.FLAG_ACTIVITY_FORWARD_RESULT like this:
FirstActivity should start SecondActivity using startActivityForResult().
SecondActivity should start ThirdActivity using this:
Intent intent = new Intent(this, ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(intent);
finish();
This tells ThirdActivity that it should return a result to FirstActivity.
ThirdActivity should return the result using
setResult(RESULT_OK, data);
finish();
At that point, FirstActivity.onActivityResult() will be called with the data returned from ThirdActivity.
Though I'd implore you to change your architecture design, it is possible to do it like this:
File ActivityOne.java
...
startActivityForResult(new Intent(this, ActivityTwo.class), 2);
...
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && data != null) {
//Collect extras from the 'data' object
}
}
...
File ActivityTwo.java
...
startActivityForResult(new Intent(this, ActivityTwo.class), 3);
...
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && data != null) {
setResult(resultCode, data);
finish();
}
setResult(RESULT_CANCELLED);
}
...
File ActivityThree.java
...
//Fill the Intent resultData with the data you need in the first activity
setResult(RESULT_OK, resultData);
finish();
...
I have 2 activities in my application, Activity1 and Activity2.When the app launches Acitivity1 is the the one that is called.Clicking on a button in Activity1 should take you to Activity2.
In Acivity2,some data processing is done then i send back data to Activity1 using an intent like this:
Intent in=new Intent(getApplicationContext(), Activity1.class);
in.putExtra("data", data);
startActivity(in);
Then getting back to Activity1 i obtain the intent data:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
String data =(getIntent().getExtras().getString("data"));
The problem here is that the first time the app launches it checks for the intent data and it does not exist so i get the nullpointerexception error.how can i make sure it checks for the intent data when Activity2 is the previous class?
You can check if the intent is existing using:
getIntent().hasExtra("data");
This will return you a boolean.
Also if oyu want to return some datas to the first activity, your should start the second one with startActivityForResult
if (extras != null) {
if (extras.containsKey("data")) {
boolean hasData = extras.getBoolean("data", false);
// TODO: Do something with the value of "data".
}
}
put a check over it
if(getIntent().getExtras()!=null){
String data =(getIntent().getExtras().getString("data"));
}
Follow the following steps
1: from your activity1's button click call as follow
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
2: In your Activity2 set the data which you want to return back to Activity1 as follow and if you don't want to return back don't set anything.
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
if you don't want to return data:
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
3: now again in your Activity1 handle the return data as follow
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
EDIT:
and if you strictly want to use your own method as in your question try this:
in your Activity1 you obtain the intent data as:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
String data ="default value";
try{
data =(getIntent().getExtras().getString("data"));
}catch(Exception e){
}
}
use ActvityForResult.
replace these codes.
in ACTVITY1:
put this after onCreate:
#Override
protected void onActivityResult(int requestCode ,int resultCode ,Intent data ) {
super.onActivityResult(requestCode, resultCode, data);
String name =data.getStringExtra("data");
if(resultCode == RESULT_OK){
switch(requestCode){
case 2:
if(resultCode == RESULT_OK){
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
}
}
}
in ACTIVITY2:
Intent intent=new Intent();
intent.putExtra("data",data);
setResult(2,intent);
finish();
So I have 2 activities lets say A and B. A navigates to B, I want the activity A to be killed or make it unusable/unseen when directed from B.
so it should be like when I press the back button on B activity it should not open activity A instead it should go to the app tray.
also activity A should comeback when I clear the application data
thanks.
You could do this in one of two ways. First is finishing ActivityA so that it can't be resumed to later. When starting ActivityB from ActivityA, you'd do something like this:
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
this.finish();
Another way is to just finish ActivityA when it gets a result of any sort from ActivityB. This code would also be in ActivityA.
To start ActivityB:
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, REQUEST_ACTIVITYB);
To make sure ActivityA doesn't resume:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_ACTIVITYB) {
finish();
}
}
REQUEST_ACTIVITYB is just an int of your choosing.
I have a problem with my code. I want to pass a String from the SecondActivity to FirstActvity. Note that the FirstActivity is not visible but its still open. when the SecondActivity is finish it passes a String to the FirstActivity.
My problem here is that when the SecondActivity ended and goes to FirstActivity, the whole application closes.
FirstActivity to SecondActivity:
Intent intent = new Intent(MainActivity.this, FileChooser.class);
startActivityForResult(intent, 0);
SecondActivity to FirstActivity:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("filePath", "/sdcard/path1");
setResult(0);
finish();
FirstActivity Result:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//TODO handle here.
Intent intent = getIntent();
this.filePath = intent.getExtras().getString("filePath");
}
What is wrong with the code?
When you set the result of your SecondActivity, you only set the result code. Instead of setResult(0) use setResult(0,intent)
Also, in your FirstActivity's onActivityResult get the extra from the data argument - this.filePath = data.getExtras().getString("filePath");
Try to use
data.getExtras().getString("filePath");
instead of
intent.getExtras().getString("filePath");`
Try with Bundle:
First Activity;
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), FIRSTACTIVITY.class);
Bundle bundle = new Bundle();
bundle.putString("filePath","/sdcard/path1");
intent.putExtras(bundle);
startActivity(intent);
}
Second Activity:
public void activity_value() {
Intent i = getIntent();
Bundle extras=i.getExtras();
if(extras !=null) {
value = extras.getString("filePath");
}
}
try this
example.It solves your problem.