My app starts from activity1, then jumps to activity2, where I get the value of a String, and jump back to activity1. The problem is, when I try to pass the String value back, I've got NullPointerException error in activity1 for getting the data value. I debugged to find the data value is correct in activity2. Please help me to set some flags to distinguish the initial launch of the app in activity1 and second time jumped back to it from activiy2. Thanks a lot!
String data = (String) getIntent().getExtras().get("data");
if(data != null){
Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "no data", Toast.LENGTH_SHORT).show();
}
You have to start activity B with startActivityForResult method and implement onActivityResult in Activity A.
Related
So i have intent that require some extras,but i was thinking is it possible to open it without passing extras and not getting null pointer?
EDIT: I have activity A and activity B. When i press button im passing few strings as extras to activity B. If its not passed,I will get null pointer and app will crash. What i want is, is it possible to check if there is extras passed,and if its passed to get them,if its not passed continue without it and not get null pointer error?
Yes, it is possible to check it. You need to use Intent.hasExtra(String name) where name is the name of the variable you want to check whether it was passed or not. For example: you place inside Intent string called firstName and then you want to use it in another activity. So here's the code you should place in the second activity:
if(Intent.hasExtra("firstName")) //Intent.hasExtra() return true or false
{
String firstName = Intent.getStringExtra("firstName");
//place here code you want to execute if intent contains firstName
}
else
{
//place here code you want to execute if intent does not contain firstName
}
Related question: Check if extras are set or not
Try this code
in A.java:
Intent intent=new Intent(A.this,B.class);
intent.putExtra("data","my custom data here");
startActivity(intent);
in B.java:
String data;
if (getIntent().hasExtra("data") && getIntent().getStringExtra("data")!=null){ //add a check like that
data=getIntent().getStringExtra("data");
}else{
//add your condition when data parameter is not there or null
}
This question already has answers here:
How to manage startActivityForResult on Android
(14 answers)
Closed 6 years ago.
From now I just put:
finish();
but it only makes me return to the parent activity and I want to pass values. Thanks in advance.
If you want to open another activity sending some string or anything use:
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
intent.putExtra("ReferencedWord","String With Whatever You Want");
int CodigoPeticion;
CodigoPeticion=2;
startActivityForResult (intent,CodigoPeticion);
finish();
referencedWord is the key to open the string in the other activity.
In Another Activity use this in your onCreate
String word="";
Bundle extras = getIntent().getExtras();
if (extras!= null) {
word = extras.getString("ReferencedWord");
Toast.makeText(getBaseContext(),"String given by MainActivity: "+word,Toast.LENGTH_LONG).show();
}
when you go form first activity to second activity.
the first activity become pause.
so when you finish the second activity the first activity is again resume
you want to pass some value from second activity to first activity
so just finish the first activity and start second activity
when you are coming back to first activity just intent from second to first activity and attached the bundle message to it after it finish the second activity
If you wanna pass any values from one activity to another activity
Intent sendVal = new Intent(this,NextActivity.class);
sendVal.putExtra("key","any values"); //key is your private key for sending values
startActivityForResult(sendVal,4); //request code
finish();
In next Activity,you can retrieve those values from PreviousActivity :
Bundle extraVal = getIntent().getExtras();
//getting values from extraVal
String myString = extraVal.getString("key"); //so our value is "any values"
Toast.makeText(getApplicationContext(),"Your string from previous Activity"+myString,Toast.LENGTH_LONG).show();
By this way,you can retrieve values from PreviousActivity.
If you wanna get values from NextActivity to PreviousActivity then override the onActivityResult method and retrieve your values from intent parameter(3rd) of onActivityResult method by matching your request code.
onActivityResult method invokes when you come back from NextActivity to PreviousActivity
I want to make a app quite similar to contacts app. In the first activity, I want to show my contact list and I have done this. First Activity consists of add new button in which on the click of that button, I want that user enter first and last name and upload an image for the same. I don't know how to navigate between activities so that i get the required answer. Can anyone please help me..??
See the first Activity at: http://postimg.org/image/996iwj5dp/9c92bd46/
and Second Activity at: http://postimg.org/image/wuscpv1tx/fe6f13c4/
on Second activity there is also a button which will make user able to choose a picture from Gallery and thats acts as 3rd Activity. I get confused in getting data from these activities.
You need to implement startActivityforResult() method in first activity.
Follow below steps to perform the operation:
1) On click of the Add new button in your First activity, instead of calling startActivity(), call startActivityforResult() using a unique activity code:
i.e: public static final int FIRST_ACTIVITY_CODE = 0;
In OnClickListener, you need to implement,
Intent m_data = (FirstActivity.this,SecondActivity.class);
m_data.putextra(//any extra data);
startActivityforResult(FIRST_ACTIVITY_CODE,data);
2) Now when you have added another user from the second activity and you want to pass some data from SecondActivity to FirstActivity, then follow below procedure:
Before you finish the SecondActivity after saving the data,
Intent m_data = getIntent();
m_data.putExtra(//data to pass in FirstActivty);
m_data.putExtra(//data to pass in FirstActivty);
.
.
.
setResult(RESULT_OK,m_data);
finish();
3) In the onActivtyResult() of the FirstActivty, do below procedure:
#Override
protected void onActivityResult(int p_requestCode, int p_resultCode, Intent p_data)
{
if(p_requestCode == FIRST_ACTIVITY_CODE && p_resultCode == RESULT_OK)
{
NewData = p_data.getExtras().get(//your data);
// update data
}
super.onActivityResult(p_requestCode, p_resultCode, p_data);
}
Hope this helps you.
Im working on my level-selector activity right now and I want to get the result from wich level I chose in my MainGameView, that is run by another activity. How would I go about doing this?
This is what Ive for when you choose level 1:
Button test1 = (Button)findViewById(R.id.test1);
test1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setLevel(1);
Intent start = new Intent("nielsen.happy.activities.HAPPYACTIVITY");
startActivityForResult(start, getLevel());
finish();
}
});
but where do I go from here? How do I get this value into my view so I can change bitmaps and other values depending on what level they chose? Is there something I need to do in my "HappyActivity"(MainGameAcitvity) to get the result? cus right now its returning 0
explenation of how games set up: Menu -> levelselector -> Game. So I need to get result from levelselector into game, so it knows what level to start.
(updated corrected response)
Don't use startActivityForResult().
Try this for Activity A:
Intent start = new Intent("nielsen.happy.activities.HAPPYACTIVITY");
start.putExtra("level", getLevel());
startActivity(start);
finish();
Then in Activity B, do this:
Extras mBundle = getIntent().getExtras();
int level = mBundle.getInt("level");
(original, incorrect response)
Lets call activity A the one that houses your sample code above.
Lets call activity B the one that you refer to as HAPPACTIVITY.
Activity A calls startActivityForResult() with a request code.
Activity B starts up and before exiting, you call setResult(int code).
When activity B finishes, A returns to the top via the method onActivityResult().
Implement an onActivityResult() and see what attributes you get.
FYI there is a condition where setResult(RESULT_OK) or setResult(RESULT_CANCELED) will not trigger onActivityResult() in A (I can't recall what the case is).
I want a clarification on intent.this is my scenario,there are 3 activities in my project namely "A","B","C".....
On clicking the button in Activity "A" ,i pass the values to "B" through bundle and based on the received value i retrieved set of values from database...Similarly "B"->"C" also...
The problem when i clicked the back button in "C" it does not proceed to "B" rather throws a error.similarly "B" to "A" also....
i found the reason that "B" can not get value from bundle when clicking Back button from "C"
Hope you understand.what's the solution....
Thanks in advance
Try starting our Activity using startActivityForResult and use setResult(int) to send the result back from Activity C to B
that true you saying bundle getting null values, so you have to prevent that null values,
like first check bundle values if not Equals null then only set
Intent myintent = getIntent();
if (null != myintent.getExtras()) {
// do your work here
String Name = myintent.getExtras().getString("Name");
} else {
// not here you can't get values
// so getting force close here
Toast.makeText(getApplicationContext(), "No Recor Here..", 12).show();
}