onActivityResult method is not being called - android

I'm having a problem in my android application. I don't know why 'onActivityResult' method is not being called when 'Up navigation' button from action bar is pressed. I think I've done everything properly:
Parent activity launch child activity with 'startActivityForResult' method.
Intent intent = new Intent(ParentActivity.this, ChildActivity.class);
startActivityForResult(intent, 1000);
Parent activity has overridden 'onActivityResult' method.
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (data != null && requestCode == 1000)
{
Bundle extras = data.getExtras();
Boolean rc = extras.getBoolean(MyConstants.INTENT_EXTRA_RESULT);
if (rc)
{
.......
}
}
}
Child activity has overridden 'onOptionsItemSelected' and calls 'NavUtils.navigateUpFromSameTask'.
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == android.R.id.home)
{
Intent result = new Intent((String)null);
result.putExtra(MyConstants.INTENT_EXTRA_RESULT, true);
setResult(RESULT_OK, result);
NavUtils.navigateUpFromSameTask(this);
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}
Child activity has overriden 'finish' method. This method set a result.
public void finish()
{
Intent result = new Intent((String)null);
result.putExtra(Constantes.INTENT_EXTRA_HAY_QUE_RECALCULAR, hayQueRecalcular);
setResult(RESULT_OK, result);
super.finish();
}
I'm not sure why 'onActivityResult' method is not being called.
What I've observed is that child activity is not being finished ('finish' method is not being called) when 'Up navigation' button from action bar is pressed. However it is called when back button (hardware button) is pressed.
What I'm doing wrong?
Thanks

As your child Activity is just on the top of your Parent Activity, no need of this method
NavUtils.navigateUpFromSameTask(this);
write like
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent result = new Intent((String) null);
result.putExtra(MyConstants.INTENT_EXTRA_RESULT, true);
setResult(RESULT_OK, result);
finish();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
finish your Child Activity when home button is pressed.

Related

Back button pressed on parent activity

in action bar when back button is pressed how do I know in the previous activity that back button was pressed , onCreate() does not get called when back button is pressed
I know from the code below that on current activity that back button was pressed but I need to know on previous activity that back button was pressed
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
You can navigate between activities using registerForActivityResult() API, and put some extra data in the returned intent that can show you from where it came from.
At source activity:
ActivityResultLauncher<Intent> launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == AppCompatActivity.RESULT_OK) {
if (result.getData() != null) {
String data = result.getData().getStringExtra("ACTIVITY_FROM");
if (data != null)
switch (data) {
case "ACTIVITY2":
// Returned from Activity2
Log.d(TAG, "onCreate: Returned from Activity2");
break;
case "ACTIVITY3":
// Returned from Activity3
Log.d(TAG, "onCreate: Returned from Activity3");
break;
}
}
}
}
);
// Go to Activity 2:
auncher.launch(new Intent(this, Activity2.class));
At the destination activity:
Then put the data when you need to return back from Activity2:
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
setIntent();
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
setIntent();
finish();
super.onBackPressed();
}
private void setIntent() {
Intent intent = new Intent();
intent.putExtra("ACTIVITY_FROM", "ACTIVITY2");
setResult(Activity.RESULT_OK, intent);
}

getIntent().getStringExtra() returns null

I have onClick method in MainActivity as following,
public void onClick(View view) {
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("I", "0");
startActivity(i);
}
In SecondActivity I validated the Intent as following
if (getIntent().getStringExtra("I") == null) {
Toast.makeText(this, "NULL", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "NOT NULL", Toast.LENGTH_LONG).show();
}
and started the ThirdActivity from SecondActivity as following
public void onClick(View view) {
Intent i = new Intent(this, ThirdActivity.class);
startActivity(i);
}
Now when come back to SecondActivity from ThirdActivity getIntent().getStringExtra("I") returns null. I also tried overriding onOptionsItemSelected as following.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent i = getIntent();
i.putExtra("I", "0");
break;
}
return super.onOptionsItemSelected(item);
}
Now don't know how to fix this issue. I don't want to use SharedPreference for this issue.
Please follow these steps:
start ThirdActivity for result
public void onClick(View view) {
Intent i = new Intent(this, ThirdActivity.class);
startActivityForResult(i, 1);
}
Update following method, add setResult(Activity.RESULT_OK, i);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent i = getIntent();
i.putExtra("I", "0");
setResult(Activity.RESULT_OK, i);
break;
}
return super.onOptionsItemSelected(item);
}
In SecondActivity, implement onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 1 && resultCode == RESULT_OK) {
// Now use data.getStringExtra("I");
}
}
You are not finishing any activity, and you are passing value from main activity to second, then calling third activity, just returning from the third activity and resuming the second activity, you are not passing any data to second activity while coming from third activity.
just change your code as follow:-
//from second to third
public void onClick(View view) {
Intent i = new Intent(this, ThirdActivity.class);
startActivity(i);
finish();
}
// returning from third to second
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent i = new Intent(this, second.class);
i.putextra("I","0");
startActivity(i);
finish();
break;
}
return super.onOptionsItemSelected(item);
}
also you can change like this:-
#Override
public void onBackPressed() {
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("I","0");
startActivity(i);
finish();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
in your third activity only
if you dont want to startactivty then startActivtyForResult() from second to third:-
startActivityForResult(i,101);
implement onActivtyResult method in second activity:-
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 101 && resultCode == RESULT_OK) {
// Now use data.getStringExtra("I");
}
}
for third activity just finish it in onBackpress() like:-
#Override
public void onBackPressed() {
Intent resultData = new Intent();
resultData.putExtra("I", "0");
setResult(Activity.RESULT_OK, resultData);
finish();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
This is because when you come back from third activity to second activity, getIntent() returns the Intent of third activity and not the first.
And while navigating from third activity to second, you are not passing any string extra and hence you get a null value.
You can try using startActivityForResult() method for your linking from SecondActivity to ThirdActivity this way, you can then use setResult(intent) before killing the ThirdActivty. Thus when you come back to your SecondActivity you will have the result intent you passed in your ThirdActivty inside the onActivityResult method of your SecondActivity.
Hope this helps.
Make a change when you move towards SecondActivity from ThirdActivity.:
Intent i = new Intent(this, SeconActivity.class);
i.putExtra("I",getIntent().getStringExtra("I"));
startActivity(i);
you have to pass again
i.putExtra("I", "0");
from third activity to second activity
try this :
put this on your thirdactivity
#Override
public void onBackPressed() {
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("I","0");
startActivity(i);
finish();
}

How to send data from Child Activity to Parent Activity in android?

In my manifest.xml, i'm used android:parentActivityName to set up parent Activity.
Here my code:
<activity
android:name=".B"
android:label="#string/title_activity_A"
android:parentActivityName=".A" >
</activity>
So, how to put data from B to A when click back button in action bar??
Because my A activity is child activity of another activity(C activity) and A activity using data from this activity(C activity)...
Somebody can help me please? Thanks and sorry because my english.
In your Activity B class write:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
Intent intent= new Intent();
intent.putExtra("param", "value");
setResult(RESULT_OK, intent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
In Activity A class add:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == 2404) {
if(data != null) {
String value = data.getStringExtra("param");
}
}
}
Start an Activity B using startActivityForResult Method
Intent intent = new Intent(A.this.getApplicationContext(), B.class);
startActivityForResult(intent, 2404);
You have to Override the actionbar back button click event & set data to parent activity with Intent parsing & handle in parent activity in onActivityResult() method.

onActivityResult is not called when the back button in ActionBar is clicked

Here is my problem:
Create a MainActivity. Add a button which will start another activity SecondActivity.
Intent i = new Intent(getActivity(),SecondActivity.class);
startActivityForResult(i,0);
Inside the SecondActivity, I capture the back button click event and also add a button to return to the first Activity.
When back button in action bar is clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// back button
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(Activity.RESULT_OK, resultIntent);
//finish();
return false;
}
return super.onOptionsItemSelected(item);
}
When the button inside activity is clicked:
Button btn = (Button)this.findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
The onActivityResult in MainActivity is called when I click the button inside the SecondActivity, but it's never been called if I click the back button in Actionbar of SecondActivity. Can anybody tell me why? Thanks
Here is the code which is working:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// back button
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I guess the finish() will close the current Activity, and return true inform that action has been processed. (The default back action seems to be different from finish().)
Try this:-
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Good answer is Gopal Rao code in the same question. Its worked for me. This is a copy of his solution:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent result = new Intent((String) null);
result.putExtra("SOME_CONSTANT_NAME", true);
setResult(RESULT_OK, result);
finish();
return true;
}
else {
return super.onOptionsItemSelected(item);
}
}

getIntent is not working inside MainActivity. Getting weird behaviour

I have one MainActivity and I have defined below code in onCreate() method. The intention is, when MainActivity gets extra String "EXIT" then show Toast message:
Intent current = getIntent();
if (current !=null && current.getStringExtra("EXIT") != null) {
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
This MainActivity starts another activity "DayOne" on some button press like:
public void processGo(View v){
Intent i = new Intent(MainActivity.this,DayOne.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
MainActivity.this.startActivity(i);
}
Now I am returning back from "DayOne" to MainActivity after putting extra string "EXIT". This I am doing inside onOptionsItemSelected(MenuItem item) method:
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent i = new Intent(DayOne.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", "EXIT");
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
The issue is, when MainActivity is getting called from DayOne with extra string "EXIT"; I am not seeing the Toast message defined in MainActivity. What is missing or wrong here?
Appreciate any help.
Thanks all for your comments and helps.
I have figured out the issue here. It was because the manifest file had entry an entry android:launchMode="singleInstance" for Both the activities (MainActivity and DayOne Activity)..
Removing it from there, it worked fine.
First check your null then get the Intent String, can you just make your code look like below within in onCreate of MainActivity#
Bundle extra= getIntent().getExtras();
if(extra!=null){
String _StrExit=extra.getString("EXIT");
if(_StrExit.equalsIgnoreCase("EXIT")){
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
}
Update
Make change while calling Intent from menuitem
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent i = new Intent(DayOne.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String _Str="EXIT";
i.putExtra("EXIT", _Str);
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
Its working for me.
MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent current = getIntent();
if (current != null && current.getStringExtra("EXIT") != null) {
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
}
public void processGo(View view) {
Intent i = new Intent(MainActivity.this, OneDayActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}
OneDayActivity:
public class OneDayActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day_one);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.day_one, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle().equals("Exit")) {
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", "EXIT");
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
}
dont clear the stack instead of if you want to do some task when you come back to your main activity (using "EXIT" string) you can achieve that using onActivityResult also.:
public void processGo(View v){
Intent i = new Intent(MainActivity.this,DayOne.class);
MainActivity.this.startActivity(i,10); // 10 is the id to handle
}
#Override
public void onActivityResult( int requestCode, int resultCode, Intent data )
{
super.onActivityResult( requestCode, resultCode, data );
switch ( requestCode )
{
case ( 10 ): // id that we pass on start activity
{
if ( resultCode == Activity.RESULT_OK )
{
Toast.makeText(this, "exiting"+data.getStringExtra( "EXIT", "" ), Toast.LENGTH_LONG).show();
}
}
break;
}
}
On finish
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent resultIntent = new Intent();
resultIntent.putExtra( "EXIT", "EXIT" );
setResult( Activity.RESULT_OK, resultIntent );
finish();
}
return super.onOptionsItemSelected(item);
}
on finish it will go back to Main Activity and call onActivityResult automatically where you can do your task.
Sorry for the typo hope it will help.

Categories

Resources