Intent on Android - android

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();
}

Related

Is it possible to open intent that require extras without passing it?

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
}

How can I return to an activity from another one and passing values in android? [duplicate]

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

Determine if data was sent via intent

Is there any way to determine if a specific boolean value was sent via an Intent? In other words, I know how to send data via an Intent and how to read it back, but I would like to know if data was sent.
The current getBooleanExtra method requires a default value, so I can't check if it wasn't sent by using this.
I currently have this:
showNavigationDrawer = getIntent().getBooleanExtra(Extras.EXTRA_SHOW_NAVIGATION_DRAWER, false);
If the Extras.EXTRA_SHOW_NAVIGATION_DRAWER value wasn't set at all, I'd like to do some extra work. Obviously if I get true it means it was sent, however if I get false there's no way to tell.
This can be done extracting the intent bundle:
Bundle b = getIntent().getExtras();
boolean hasNavDrawerSetting = b.containsKey(Extras.EXTRA_SHOW_NAVIGATION_DRAWER);
if (hasNavDrawerSetting) {
showNavigationDrawer = getIntent().getBooleanExtra(Extras.EXTRA_SHOW_NAVIGATION_DRAWER, false);
} else {
showNavigationDrawer = getResources().getBoolean(R.bool.hasSideMenu);
}
If you really want to check whether your value is coming or not then you can take help of Bundle object. You can pass your boolean value through a bundle object. If the bundle object is null then no value is received in the next activity. But if it is not null then you will surely receive the value of your boolean parameter only if you put it in the bundle. It may be true or false depending upon the value you set. Below I'm providing two code snippets. One is for calling activity and another is one for called activity.
Calling Activity -->
Intent intent = new Intent(this,Experimental.class);
Bundle b = new Bundle();
b.putBoolean("key",true);
startActivity(intent.putExtra("bundle",b));
Called Activity -->
setContentView(R.layout.experimental);
Bundle b = getIntent().getBundleExtra("bundle");
if(b != null){
Log.d("Value",String.valueOf(b.getBoolean("key",false)));
}
Hope I'm able to help you.

Android NullPointerException for passing parameter from Activity

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.

startActivityForResult(), how does it work?

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).

Categories

Resources