How to end parent activity from child activity without ending / stopping child activity?

I am starting an android. Can someone help me with an example?

While I was looking for him, but did not find useful things. I just discovered that to close parent activity, you also need to close child activity using the setResult () method.

What exactly I'm looking for is that my parent activity should be closed from child activity, not ending child activity.

+4
source share
6 answers

From a child action, try using the getParent () method, which returns the parent activity and completes () it!

+1
source

The back activity stack works like a stack, i.e. you can click on it actions. You cannot delete an action from the stack without first deleting the stack above it. Therefore, it is possible that a child cannot finish his parental activity without completing himself.

There are several ways you can use to reorder or even clear the activity stack using launchmodes and intent flags .

+1
source

Create the static variable Context as the static Context cntxofParent; in ParentActivity

Then initialize this link in Parent Activiy, for example

cntxofParent = ParentActivityName.this;

When you are in a child activity, then collect this Context and translate it into Activity, as shown below,

Action parentActivity;

parentActivity = (activity) cntxofParent;

+1
source

I suggest you read about Task and Back Stack

To start, the actions in android are pushed onto the stack called a, if my memory serves me correctly, Activity Stack. When you start an action, you can add an intent flag that will affect the stack, for example

addFlags (Intent.FLAG_ACTIVITY_CLEAR_TASK)

I think I did what you are trying to do before (I'm not sure which flag I used, it was a long time ago: p). But just read about the activity task to find out which flag of intent fits what you are trying to do.

0
source

Activity A → Activity B, delete Activity A,

if this is what you are looking for

Intent intent = new Intent(this, B.class); startActivity(intent); finish(); 
0
source

Termination of parent activity from a child activity without completion of a child process. Activity is not a difficult task ... you must declare one activity variable in the parent activity.

 static HomeScreen firstActivity; //Activity Variable In oncreate method assign the variable firstActivity=this; public static HomeScreen getActivity() { return firstActivity; } 

And access the parent variable Activity in the child activity.

 HomeScreen.getActivity.finish(); 

Click here for more details.

0
source

Source: https://habr.com/ru/post/1434451/


All Articles