Android - How to finish a child activity from a parent activity - android

I have a first activity A where I start a new activity B with:
intent.setClass(A.this, B.class);
Now, after some event, I would like to finish B from A (A contains a running thread).
How could I reach this?
Thanks in advance.

you can send broadcast message from A to B
Do you need to use B as Activity instance in these terms? may be better choice is to create B as dialog?

You don't. The Activity will be finished by the OS when memory resources are low.
Also, clicking the back button calls finish() on the current activity and displays the top activity in the backstack, so presumably you will have already finished the child.

Related

Switching between activities without destroying the activity

I am new to android so don't know what can be the solution to this problem.
I have created an app with two activities in it named these as first and second activity.
First activity launch mode is singleTask and second activity launch mode is standard.
Now when i switch from second activity to first activity i don't want to destroy the second activity.
But in this case it is getting destroyed.
So can anyone help me in suggesting how can i achieve this scenerio of switching from second activity to first without
destroying second activity.
This is because activity one is single task activity.
Read here. According to this
"singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.
To avoid this scenario, change launch mode of activity one to standard.
No need to destroy any activity for this, i think so. Just implement this:
Intent intent=new Intent(ActivityOne.this,ActivityTwo.class);
startActvity(intent);
in your oncreate() method of actvity
You can open new activity that you want to open, without finish() your current / previous activity by using finish().
Once you open multiple activity without finish() previous ones, the OS will automatically maintain the activity stack.

Finish activity AFTER (delay) starting a new one

Not sure how to do this:
Current activity is: A
I want to start activity: B
But I want that activity B's UI should load completely before Activity A is finished.
What I need:
Current activity is: A
Start new activity: B
Activity B loads completely
Activity A finishes
I need this because activity B's UI is translucent when it starts. After an animation, the activity's background becomes opaque. While this is happening, the homescreen is showing because activity A finishes quickly.
Thank you in advance.
Here is one not-so-straightforward approach at the top of my head. You can use something called LocalBroadcast Manager. It would be like a message from Activity B to Activity A saying that 'Hey, I've finished loading the animation. Now I don't need you!".
So, before starting a new activity your activity A can start listening for this local broadcast and register a receiver. Then when on your activity B the animation finishes, you can send a Local Broadcast message saying "I don't need you"(not literally). This will be received by receiver in Activity A, where you can finish it.
See how to use LocalBroadcastManager? for how to implement it easily. Hope it helps you.

finish() not working properly

In my app, whenever calling the finish() method, wherever it was, I am not taken to the previous activity, rather I am directed to mainActivity.
finish();
My aim is, showing the user the activity just before the current activity he is seeing.
Question 1 : How can I make finish() always take me to the activity before ?
Question 2 : Does this work using another workaround other than finish() ?
Question 3 : How to check the stack of activities and decide accordingly which one to go to ?
If you have written finish in each intermediate activity, that means you are removing the activity from the stack, hence on finishing an activity you are taken to the last non-finished activity, hence write finish() in only that activity which you do not want to see until the same workflow is followed and its onCreate() is called
if you start activity c from b and b from a if you use the back button on your phone at activity c it will go to b and back button press in b it will show a. if you use finish in all the 3 activities what happens is a calls b and a is finished and b calls c and b is finished so when you use finish in c it will not have b to show. so you have to tell where you are placing your finish based on some condition or a button click or just before starting a new activity, post your code and we will help you.

Android how to create activity only if it wasn't created yet

I have a problem cause when I go to different activities using startActivity function,
they always get created from scratch. Even if activity A was visited before, when I go to activity B and then A again, activity A is created again.
The problem is with back button, cause if I go to Activity A then B then A and then B,
in order to close the application I have to press back button 4 times.
I guess that it shouldn't act like it and user should be able to go to activity A when first pressed back button and the second press should close the application.
How to solve this issue?
Greetings
If you have activity transitions like:
Activity A -> Activity B
Activity B -> Activity A
and you want the user to go back to the same instance of Activity A in this case, maybe you just need to call finish() in Activity B after you call startActivity() for Activity A?
If this isn't helpful, please give us more information about what you are trying to do.
make sure you implement onSaveInstanceState and be prepared to restore your activity from a Bundle in onCreate. that's how you re-establish where you were when you return to an activity.
add launcheMode="singleTask" to your activity in the manifest
You need to set FLAG_ACTIVITY_SINGLE_TOP to your intent for launching activity A. Doing so will cause your previously created activity to re-use. Make sure you do handle your afterwards intents in onNewIntent method. For more info.
You need to set the flag FLAG_ACTIVITY_REORDER_TO_FRONT when you start activity A from B or vice versa, like
i = new Intent("....ActivityAorB");
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
I've tried solutions proposed so far, however they didn't do it for me.
What did however, is using flag FLAG_ACTIVITY_CLEAR_TOP while starting activities.
Thanks for pointing me in the right direction though.

OnClick restartActivity?

How can I restart An Activity on click?
For exemple: I have got in my AndroidManifest.xml 2 activities the activity A and B and they start when application starts...
But what I want is when I click in a button that is on Activity A it must restart activity B.
when you are in activity A, and proceeding to activity B, then your activity B automatically starts/re-starts
Why do you need to start both Activities on starting your app? When you say "restart", do you actually need to stop Activity B and start Activity B again? Or do you just want to show it? To start an Activity from another Activity, you could call something like this:
startActivity(new (Intent(this, ActivityB.class)));
The Android documentation gives plenty of detail. However, I think you should consider why you are starting two activities at once, and whether you might want to use a Service instead (not knowing any details of your app, I can't say).
Intent intent = new Intent(CurrentActivity.this, ActivityToLaunch.class);
startActivity(intent);
call above piece of code on onClick of view method.

Categories

Resources