So I have 2 activities lets say A and B. A navigates to B, I want the activity A to be killed or make it unusable/unseen when directed from B.
so it should be like when I press the back button on B activity it should not open activity A instead it should go to the app tray.
also activity A should comeback when I clear the application data
thanks.
You could do this in one of two ways. First is finishing ActivityA so that it can't be resumed to later. When starting ActivityB from ActivityA, you'd do something like this:
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
this.finish();
Another way is to just finish ActivityA when it gets a result of any sort from ActivityB. This code would also be in ActivityA.
To start ActivityB:
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, REQUEST_ACTIVITYB);
To make sure ActivityA doesn't resume:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_ACTIVITYB) {
finish();
}
}
REQUEST_ACTIVITYB is just an int of your choosing.
Related
I am using 5 activities. from activity1 I move to activity2, from act2 to act3, from act3 to act4 and from act4 to act5.
activity 2 carries data to act3 and like this act5 receives data of act2, act3, act4 and then send all data to act 1.
My First activity
I{
Intent i= new Intent(firstactivity.this, secondactivity.class);
startActivityForResult(i, 10);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10) {
String a= data.getStringExtra("Value1");
String b= data.getStringExtra("Value2");
String c= data.getStringExtra("Value3");
String d= data.getStringExtra("Value4");
String showall = a+", "+b+", "+c+", "+d;
address.setText(showall);
}
My Second activity
Intent intent = new Intent(secondactivity.this, thirdactivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent.putExtra("Value1", firstvalue);
startActivity(intent);
My Fifth activity
Intent intent = new Intent(fourthactivity.this, fifthactivity.class);
intent.putExtra("Value1", geta);
intent.putExtra("Value2", getb);
intent.putExtra("Value3", getc);
intent.putExtra("Value4", getd);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
setResult(10);
finish();
Try below:
Intent intent = new Intent(fifthactivity.this, firstactivity.class);
intent.putExtra("Value1", geta);
intent.putExtra("Value2", getb);
intent.putExtra("Value3", getc);
intent.putExtra("Value4", getd);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent);
And then process your passed result in onCreate (in case activity got destroyed) or onNewIntent (if your activity is still running but you bring it to front and update it with new intent) of your firstactivity
//A simple approach to solve this issue is to use sharedpreferences
//store value from activity five
getsharedpreferences('temp','MODE_PRIVATE').edit().clear().putString('values','new value').apply();
//get values from shared preferences if its available
String value 5 = getsharedpreferences('temp','MODE_PRIVATE').getString('values','nil');
getsharedpreferences('temp','MODE_PRIVATE').edit().clear();
first activity
static ArrayList<String> arlist=new ArrayList<String>();
arlist.add("value");
arlist.add("value1");
arlist.add("value2");
In second Activity
ActivityOne.arlist
I am using StartActivityForResult for multiple activities. My main activity is where I initialize it. On my second activity I input some values and pass to a third activity. Now, When i'm on the third activity I want to be able to go back to the second activity if ever I want to edit the values i passed. What should I do?
MainAct.java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE)
{
if (resultCode == Activity.RESULT_OK)
{
//Something
}
}
SecondAct.java
Intent vd2 = new Intent(ViolatorDetails1.this,ViolatorDetails2.class);
vd2.putExtra("name",name);
vd2.putExtra("lname",lname);
vd2.putExtra("lnumber",lnumber);
vd2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
vd2.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(vd2);
finish();
ThirdAct.java
Intent intent = new Intent();
intent.putExtra("firstname",name);
intent.putExtra("lastname", lname);
intent.putExtra("licensenumber", lnumber);
setResult(Activity.RESULT_OK, intent);
finish();
How can I go back to second Activity from third activity to edit some values if ever?
You should not call finish() on second activity when starting the third one.
Then onActivityResult() will be call when third activity is finished.
Call
startActivityForResult(vd2);
instead of
startActivity(vd2);
simply remove finish();
Intent vd2 = new Intent(ViolatorDetails1.this,ViolatorDetails2.class);
vd2.putExtra("name",name);
vd2.putExtra("lname",lname);
vd2.putExtra("lnumber",lnumber);
vd2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
vd2.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(vd2);
finish(); //remove this line
in this way when your third activity closes user will go back to 2nd one, also you should implement onActivityResult in your 2nd activity so you can handle weather user wants to edit or has finished and should go back to 1st activity! (i.e. setting the result of the intent came from 1st activity and finish the 2nd one!)
here is what I mean in code:
In your 2nd activity do this,
#override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE)
{
if (resultCode == Activity.RESULT_OK)
{
// user should have done his job on 3rd activity and we should finish the 2nd activity to go back to first activity!
}else{
//user still editing!
}
}
and instead of startActivity(vd2); do startActivityForResult(vd2);
I have two activities, one which is transparent with a second activity showing underneath it. Is it possible to tap the transparent activity, dismiss it and have the tap carry through to the activity underneath?
Not sure what you mean exactly by "carry through", but this is what you can do in Android.
Start Activity A. When starting Activity B, start as such:
Intent intent = new Intent(A, B);
startActivityForResult(intent, SOME_RESULT_CODE);
Then when you dismiss Activity B, aka call finish(), you need to make sure you send a result:
Intent goBackToActivityA = new Intent();
goBackToActivityA.putExtra(PUT_EXTRA_FOR_WHATEVER_YOU_WANT_IN_A);
setResult(Activity.RESULT_OK, goBackToActivityA);
finish();
And then when you get back to Activity A, you will need to capture that result and handle it:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SOME_RESULT_CODE) {
if(resultCode == Activity.RESULT_OK){
// extract PUT_EXTRA_FOR_WHATEVER_YOU_WANT_IN_A here
// whatever the "carry through" is you can manage here
}
}
}
I have a Fragment that contains a RecyclerView which uses a custom RecyclerAdapter. I have an onClickListener inside my custom RecyclerAdapter - when a position is clicked I want it to start startActivityForResult. So far this works in as such as when it is clicked it starts the Activity as desired. However when I press the back button to go to the Fragment containing the RecyclerView onActivityResult is never called. I have passed in a context to the custom RecyclerAdapter. Is this something that is possible? Or does the Activity/Fragment initiating startActivityForResult be the one that intercepts it? If not I will end up handling the onClick in the Fragment with a gesture detector or something similar, but before that I wanted to give this a fair crack! Note: I have included onActivityResult in the MainActivity which has the Fragment container so the Fragment does receive onActivityResult if startActivityForResult is initiated from the Fragment. My code:
RecyclerAdapter onClickListener:
#Override
public void onClick(View v) {
String titleId = titlesListDataArrayList.get(getLayoutPosition()).getTitle_id();
Intent intent = new Intent(context, CreateItemsActivity.class);
intent.putExtra("TITLE_ID", titleId);
((Activity) context).startActivityForResult(intent, Constants.NEW_ITEMS_REQUEST_CODE);
}
CreateItemsActivity.class - onBackPressed()
#Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(Constants.NEW_ITEMS_REQUEST_CODE, intent);
finish();
}
MyListsFragment.class (contains RecyclerView)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("CALLED", "OnActivity Result");
// if requestCode matches from CreateItemsActivity
if (requestCode == Constants.NEW_ITEMS_REQUEST_CODE) {
Log.e("REQUEST CODE", String.valueOf(requestCode));
populateRecyclerView();
}
}
Also I have this in the MainActivity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// included to allow fragment to receive onActivityResult
}
Your calling Activities onActivityResult will only be called when the second activity finishes and a setResult has been executed.
Since the user is hitting the back button the 2nd activity is finishing without setResult being called.
You'll need to override onBackPressed so you can execute your setResult code.
I see you have implemented this but I think the crux of the issue is that you need to return Activity.RESULT_OK not your request code.
#Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
super.onBackPressed();
}
In this case you don't need to explicitly return your requestCode of Constants.NEW_ITEMS_REQUEST_CODE because Android will forward that automatically.
OK, so IF by any chance somebody else has this problem here is my
solution. I added this code into the MainActivity onActivityResult (note I have a frame container which is where all fragments are inflated):
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// get current fragment in container
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameContainer);
fragment.onActivityResult(requestCode, resultCode, data);
}
I believe this works as the MainActivity is top in the hierarchy and intercepts the onActivityResult, so basically I just point it where I want it to be used.
I am currently trying to send data from a child activity back to its parent activity. I have used the startActivityForResult() to go to my child activity. However I cannot understand what I would do to put the data I want to put back to my intent and make my parent activity receive it. I have been looking at different examples online but I think it is only throwing the result variable.
As per my understanding this is what I used when I return my child activity back to the parent activity:
String somestring = "somevalue";
Intent i = getIntent();
setResult(RESULT_OK, i);
finish();
I want to load the content of the string somestring for it to return back to the parent activity.
How can I load it back to my parent activity?
startActivityForResult(intent, 1);
And lastly how can I capture the data in my parent activity in the onActivityResult?
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
}
Add your data to an intent, just as you would in any other situation:
Intent intent=new Intent();
intent.putExtra("ComingFrom", "Hello");
setResult(RESULT_OK, intent);
finish();
Then retrieve it in onActivityResult in your other activity.
#Override
public void onActivityResult(int requestCode,int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
String extraData=data.getStringExtra("ComingFrom"));
}