I have total 3 activites. I pass the data from the first activity like this:
Intent i = new Intent(getApplicationContext(), History.class);
i.putExtra("day", mDay);
i.putExtra("month", mMonth+1);
i.putExtra("year", mYear);
startActivity(i);
get the data:
Bundle extras = getIntent().getExtras();
if(extras !=null) { .......
now I pass the data from the second activity:
Intent i = new Intent(getApplicationContext(), History.class);
i.putExtra("Hday", mDay);
i.putExtra("Hmonth", mMonth);
i.putExtra("Hyear", mYear);
startActivity(i);
and get the data from the second activity:
Bundle extras2 = getIntent().getExtras();
if(extras2 !=null) {
So, I have extras to get the data from the first activity, and extras2 to get the data from the second activity. But when I pass the data from any activity both extras are not null!
What have I done wrong?
The problem is that your third activity has no way of knowing which activity sent the bundle. You need to add a field that identifies what type of bundle this is so you can process accordingly. For instance, in activity 1:
Intent i = new Intent(getApplicationContext(), History.class);
i.putExtra("activity", (int)1);
i.putExtra("day", mDay);
i.putExtra("month", mMonth+1);
i.putExtra("year", mYear);
startActivity(i);
Then in 3rd activity:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
int typeAct = extras.getInt("activity");
if (typeAct == 1) {
.......
pass the data in a one bundle
the first activity would for example have three values in the bundle then the second one retrieves the data from the bundle and addsor 'Append' to the second activity and finally in the third activity you can retrieve the data as a single bundle from the second activity.
Related
i am trying to use startactivityfromfragment()
i didnt find sumthing usefull for me to be a good example
until now i was able to move data(string of web url) from fragment to fragment
now its a diffrent story.. i think i need to use startactivityfromfragment()..
but dont know how
Bundle bundle = new Bundle();
bundle.putString("WEB","someweb.com");
MultiFragment multiFragment = new MultiFragment();
multiFragment.setArguments(bundle);
Intent intent1 = new Intent(getBaseContext(), MultiFragment.class);
startActivityFromFragment(MainActivity.class,intent1,"WEB");
Try this:
Put this code in current fragment, from where you want to open an activity.
Intent i=new Intent(getActivity(),Multifragment.class);
i.putExtra("key","value");
getActivity().startActivity(i);
In the next activity which contains fragment, put these:
String value=getIntent().getStringExtra("key");
Bundle bundle=new Bundle();
bundle.putString("key",value);
YourFragmentClass ob=new YourFragmentClass();
ob.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.your_container_layout,ob).addToBackStack(null).commit();
Then, In YouFragmentClass:
Bundle=getArguments();
if(bundle!=null}
String value=bundle.getString("key");
Use your Activity name as second parameter in which the your MultiFragment exist
Intent i = new Intent(MainActivity.this, ActivityName.class);
putExtras(bundle);
startActivity(i);
finish();
Method 1
For sending data from activity to another activity's fragment call a normal intent with bundle value, In another activity check on getIntent() and call a method from that fragment
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("web_url", "url");
startActivity(intent);
and call from another activity
String webUrl = getIntent().getStringExtra("web_url");
Fragment newFragment = getSupportFragmentManager().findFragmentById(R.id.frame_tab);
newFragment.openWeb(webUrl);
Method 2
In second activity make a static variable and save the data from getIntent() and in fragment use that variable.
Mehtod 3
interface FragmentListner{
void openFragment(String url);
}
Activity A :
FragmentListner frag = new FragmentListner();
frag.openFragment("Url");
Activity B:
class B implements FragmentListner{
void openFragment(String url){
//Open Fragment here
}
I think:
In an activity that doesn't contain fragment:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("something", "yep");
startActivity(intent);
And, in the next activity which contains fragment:
Intent intent = getIntent();
String op = intent.getStringExtra("something");
if (op != null){
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new YourFragment()).addToBackStack(null).commit();
}
I have a List Activity that when the user presses a line item, it passes the ID of the record to the next activity.
Intent intent = new Intent(CustomerListActivity.this, CustomerEditActivity.class);
intent.putExtra("CUSTOMER_ID", id);
startActivity(intent);
I can see the data in the intent when debugging on that activity; however, when I get into the next activity, the data is not coming up with the below code.
Intent i = getIntent();
Bundle b = i.getExtras();
String s = b.getString("CUSTOMER_ID");
I have debugged and poked around in the variables window, but I do not see the Customer_ID=# value as I do on the previous activity.
You should call String s = b.getStringExtra("CUSTOMER_ID");
Try this.It will work.Paste it on your next activity where you want to get data from intent.
String s= getIntent().getExtras().get("CUSTOMER_ID")+"";
This is how you can do it in a right way
//main activity
Intent intent = new Intent(getActivity(), TargetActivity.class);
intent.putExtra("ParamKey", "key_value");
getActivity().startActivity(intent);
****
//TargetActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target_activity_layout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.get("ParamKey") != null)
paramValue= extras.getString("ParamKey", "default_value");
}
You will have to call getStringExtra() method in New Activity to retrieve the value from Intent.
For example :
String custIdInNewAct= getIntent().getStringExtra("CUSTOMER_ID");
Bundle contains data sent by the calling activity so
Bundle b = getIntent().getExtras();
String s = b.getString("CUSTOMER_ID")`;
I need to from Activity A start an Activity B for result. I need to pass a String ArrayList from Activity A to Activity B first. I thought that this code would work but it crashes the app with a message that the list was not passed:
Activity A:
Intent intent = new Intent(MainActivity.this,PopUpRunda.class);
Bundle sendList = new Bundle();
sendList.putStringArrayList("list",listA);
startActivityForResult(intent,2,sendList);
Activity B:
Bundle gotList = getIntent().getExtras();
ArrayList<String> listB = gotList.getStringArrayList("list");
Replace:
Intent intent = new Intent(MainActivity.this,PopUpRunda.class);
Bundle sendList = new Bundle();
sendList.putStringArrayList("list",listA);
startActivityForResult(intent,2,sendList);
with:
Intent intent = new Intent(MainActivity.this,PopUpRunda.class);
intent.putStringArrayListExtra("list",listA);
startActivityForResult(intent,2);
The Bundle that is available as a parameter on startActivityForResult() is not how you pass Intent extras.
I am attempting to pass an int through an intent to another class and have managed to successfully pass through the integer however I am unsure as how to convert a Bundle to an Integer.
Code from Intent:
private void nextPage()
{
Intent intent = new Intent(this, Timer.class).putExtra("totalTime", totalTime);
startActivity(intent);
}
Code in Timer class:
Bundle time = getIntent().getExtras();
if(time == null)
{
timeDisp.setText("Failed.");
}
else
{
totalTimeMs = Integer.parseInt(String.valueOf(time));
timeDisp.setText(totalTimeMs);
}
Thanks in advance :)
Intent can hold directly all java primitives types and parcelable/serializable objects.
You may have a confusion that it can also hold Bundles.
Do you really need to put your integer in a Bundle? It can be true for multiple values that logically coupled.
Check Intent API.
If your totalTime is of type int which you pass through putExtra() the you can use:
int time = getIntent().getExtras().getInt("totalTime");
You are not adding your intent to a Bundle, so on the recieving activity you are trying to get data out of an empty Bundle.
You would Add the data to a bundle by:
private void nextPage()
{
Intent intent = new Intent(this, Timer.class);
Bundle b = new Bundle():
b.putString("totalTime", totaltime);
intent.putExtras(b);
startActivity(intent);
}
Then you would retrieve the String from the bundle:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String time = extras.getString("totalTime");
I have 3 activities we will call them A, B and C. A and B both have intents that send the view to C. And design on which one C came from different things happen on C's activity. I pass the data like this from A to C:
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "A");
startActivity(intent);
Then in C's onCreate method I have something like this:
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
Then I from B to C I have
Intent intent = new Intent(this, C.class);
startActivity(intent);
And then I get the NullPointerException, I am guessing this is because i have not specifed a String "Action" when going from B to C.
Now I could just add one line for B in this case but, if there were for more activities or a large project this would not be good, because for each activity going to one I would need this.
How can I have it so I don't get this exception without adding an "Action" sting to activity B?
Thanks for the help in advance.
EDIT
If I have this from A to C
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "A");
intent.putExtra("Action2", "A");
startActivity(intent);
And this from B to C
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "B");
startActivity(intent);
And then this in onCreate of C it fails when going from B to C:
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
else if (extras.getString("Action2").equals("A")) {
//DO Stuuf
}
}
Change the code in C class like this for checking the bundle is null or not.
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.getString("Action") != null)
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
}
if(extras.getString("Action2") != null)
if (extras.getString("Action2").equals("A2")) {
//DO SOMETHING
}
}
}
check below code :
if(getIntent().hasExtra("Action"))
//if you have passed "Action" from activity
else
//if you did not pass "Action" from activity
Try this...
Bundle extras = getIntent().getExtras();
if(extras != null){
if (extras.getString("Action").toString().equals("A") || extras.getString("Action").toString() == "A") {
//DO SOMETHING
}
}
Hope this solves
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getString("Action").equals("A")) {
//DO SOMETHING
}
you are always creating new Intent object when ever you are moving to new activity. Hence this issue. Try to copy required data in new intent and access, them.
NullPointerException This will Happen because When you are moving from Activity A to C you are passing intent with value and in activity C you are getting that value using Bundle. But When you Move From Activity B to C you are not passing values through intent and in Activity C you have Written this
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
at this tme the extra is null So you will get Error.
DO like this
Bundle extras = getIntent().getExtras();
if(extras != null)//This one will check for null
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
EDIT
For passing Multiple values from activity
Intent intent= new Intent(this,NextActivity.class);
Bundle extra = new Bundle();
extra.putString("Action1","Value1");
extra.putString("Action2","Value2");
intent.putExtras(extra);
startActivity(intent);
In NextActivity
Intent i = getIntent();
Bundle extras = i.getExtras();
String strAction1 = extras.getString("Action1");
String strAction2 = extras.getString("Action2");
You are starting the Activity C twice( whenever you call startActivity(intent) ). First time from A and second time from B. Both times you are checking this
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
Which is bound to give you a null pointer exception. Put up a break point in your IDE and verify it.
why don't you use some static variable to share the info between the activities in case you don't want to instantiate the class C more than once.