Hi!
Today in this blog i have explain about the difference between startActivity & startActivityForResult()..,
in android activity means an action / screen, when we wants to move to next action / screen we are using startActivity() menthod.
The startActivity() is not only used for migration and it also can carry data within..,
Intent intent = new Intent(1stActivity.this, 2ndActivity.class);
//here putExtra() method is used for carry some data
intent.putExtra("data_name", actual_data);
startActivity(intent);
Now directly we can forward the data from one activity to another activity.
But if you wants to redirect to the 1st activity from the 2nd activity with data of 2nd activity we can use number of technology in Android like
The Best & Simple way is to use startActivityForResult() method
Step-1 :
(In your 1st Activity class)
Use the syntax below When you want to move from the 1st Activity to the 2nd Activity
use startActivityForResult() instead of using startActivity()
Intent intent= new Intent(1stActivity.this, 2ndActivity.class);
startActivityForResult(intent, 1);
//here we are passing two arguments the 1st one is Intent & the 2nd one is integer value for specification.
Step-2 :
(In your 2nd Activity class)
Use the Syntax below When you want to pass your data to the 1st Activity from the 2nd Activity
Intent i=new Intent();
i.putExtra("data_name",actual_data);
setResult(1, i);
//here we are passing two arguments the 1st one is Intent & the 2nd one is integer value for specification.
finish();
Step-3 :
(In your 1st Activity class)
// override the onActivityResult() method
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//check with the int code specification
//(in above activity we are using the integer value of "1")
if (requestCode == 1)
{
if (null != data)
{
String lst_of_sr = data.getStringExtra("data_name");
Toast.makeText(getApplicationContext(), lst_of_sr Toast.LENGTH_LONG).show();
}
}
}
// Custom Been Class
Thank you..,
Have A Happy Day..,
Today in this blog i have explain about the difference between startActivity & startActivityForResult()..,
Activity;
in android activity means an action / screen, when we wants to move to next action / screen we are using startActivity() menthod.
The startActivity() is not only used for migration and it also can carry data within..,
Intent intent = new Intent(1stActivity.this, 2ndActivity.class);
//here putExtra() method is used for carry some data
intent.putExtra("data_name", actual_data);
startActivity(intent);
Now directly we can forward the data from one activity to another activity.
But if you wants to redirect to the 1st activity from the 2nd activity with data of 2nd activity we can use number of technology in Android like
- Preference,
- with the help of Bean,
- startActivityForResult()
- & etc..,
The Best & Simple way is to use startActivityForResult() method
Step-1 :
(In your 1st Activity class)
Use the syntax below When you want to move from the 1st Activity to the 2nd Activity
use startActivityForResult() instead of using startActivity()
Intent intent= new Intent(1stActivity.this, 2ndActivity.class);
startActivityForResult(intent, 1);
//here we are passing two arguments the 1st one is Intent & the 2nd one is integer value for specification.
Step-2 :
(In your 2nd Activity class)
Use the Syntax below When you want to pass your data to the 1st Activity from the 2nd Activity
Intent i=new Intent();
i.putExtra("data_name",actual_data);
setResult(1, i);
//here we are passing two arguments the 1st one is Intent & the 2nd one is integer value for specification.
finish();
Step-3 :
(In your 1st Activity class)
// override the onActivityResult() method
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//check with the int code specification
//(in above activity we are using the integer value of "1")
if (requestCode == 1)
{
if (null != data)
{
String lst_of_sr = data.getStringExtra("data_name");
Toast.makeText(getApplicationContext(), lst_of_sr Toast.LENGTH_LONG).show();
}
}
}
To Transfer Object Between Activities!
// Custom Been Class
public class List_Of_List_B implements Serializable { String food; public List_Of_List_B() { } public List_Of_List_B(String food) { this.food = food; } public String getFood() { return food; } public void setFood(String food) { this.food = food; } }// Array list for Custom Class
ArrayList<List_Of_List_B> sing_mod = new ArrayList<List_Of_List_B>();
sing_mod.add(new List_Of_List_B("Conversion van")); sing_mod.add(new List_Of_List_B("Cutaway van chassis")); sing_mod.add(new List_Of_List_B("Mail order")); sing_mod.add(new List_Of_List_B("Multi-stop truck")); sing_mod.add(new List_Of_List_B("Cargo"));
// From 1st Activity
now you can back to the 1st activity with the data of 2nd activity.Intent int_po_trkr = new Intent(PurchaseOrder.this, List_Of_List.class); int_po_trkr.putExtra("commom_list", sing_mod); startActivityForResult(int_po_trkr, 1);// On Second Activity
ArrayList<List_Of_List_B> temp_list= (ArrayList<List_Of_List_B>) getIntent().getSerializableExtra("commom_list");
Thats All..,
Thank you..,
No comments:
Post a Comment