1

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.

Athena
  • 476
  • 1
  • 4
  • 12
  • You might want to look into animation resources and overridePendingTransaction(). Have Activity wait before it slides out. Not sure if it will work but may. If not you can implement Activity B as a Fragment instead and insert it into a RelativeLayout overlay. – zgc7009 Aug 21 '14 at 01:30
  • You could try wrapping `Activity#finish()` with a `Handler#postDelayed()` method. By curiosity, why do you want to close `Activity A` manually? – Leandro Aug 21 '14 at 01:47
  • @zgc7009 I tried `overridePendingTransition` but the exit animation only delays the start of new activity. It does not allow an overlap. Fragment seems like the only other solution. But that would mean converting all my activities to fragments - I am trying to avoid this. – Athena Aug 21 '14 at 01:55
  • @Leandro Is using `Handler#postDelayed()` after the activity is in a paused state correct? Both activity A and B are disjoint activities, accessible only through the navigation drawer. Pressing back in activity B should not take the user to activity A - thus, the `finish()` call. – Athena Aug 21 '14 at 02:00
  • You could send a broadcast from B to A that is triggered in onStart() of B and when A receives it, A finishes. – zgc7009 Aug 21 '14 at 02:00
  • @zgc7009 From what I have learnt, `broadcastreceivers` should be registered in `onResume()` and unregistered in `onPause()`. How will activity A receive the broadcast since its already past `onPause()`? – Athena Aug 21 '14 at 02:06
  • @Athena My solution, even if it works, doesn't feel right. But I asked the reasoning behind your intention to understand your need and suggest something more adequate. If I understood it correctly, them you don't need to close `Activity A`, just avoid returning to it if the back button is pressed. If you opened `Activity B` to just do one thing, them you can do `Activity#startActivityForResult()`, do what you need in `Activity B` and override `Activity#onActivityResult()` in `Activity A` to close it. – Leandro Aug 21 '14 at 02:07
  • @Leandro I don't think `startActivityForResult` will work in my case. Each activity in the navigation drawer (7 in total) is independent in functionality. So, whenever any of them is launched, they _must_ remain at the root of the task. For example, when B is launched, B must be the root. Same goes for any other activity. – Athena Aug 21 '14 at 02:13
  • Man, didn't even think about that. I'm trying to come up with a stretch idea so you don't have to use fragments but I really think that will achieve the result you want. You might be able to create some sort of global stack counter that increments each time you start an activity and from your root activity, then backing out and hitting onResume() finishes the activities in the stack and decrements the global stack counter until it's back to 0 (your root activity). – zgc7009 Aug 21 '14 at 02:25
  • @zgc7009 I really appreciate your help :). Switching to fragments might be the only reasonable solution to this. Thank you. – Athena Aug 21 '14 at 02:32

1 Answers1

2

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.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • From what I have learnt, `broadcastreceivers` should be registered in `onResume()` and unregistered in `onPause()`. Isn't that so? – Athena Aug 21 '14 at 02:03
  • 1
    True in general. They say it to as onPause will always be called. `onDestroy()` might not be called only for the case when app/activity is killed by the system to free memory. Even then that shouldn't be a issue with your case as there would be no activity in any case then. For `Local Broadcast` you can unregister just after you receive a message from Activity B. Since you'll be calling it for sure after your animation finishes, it shouldn't create an issue. Calling unregister n `onPause` shouldn't be necessary for this case in my view. – Shobhit Puri Aug 21 '14 at 02:13
  • Ok. I will give this a go and get back to you. – Athena Aug 21 '14 at 02:14
  • Seems to be working fine.. pending further tests on different devices/apis. Thanks man. – Athena Aug 21 '14 at 02:27
  • Sure. Please do comment if you find any issue with this. It will help me as well :) – Shobhit Puri Aug 21 '14 at 02:30
  • Yea saw this answer after I posted my comment, thought about it more, and agree. You are sending your broadcast so soon after you pause the activity that the likelyhood you would run into a leak is highly unlikely. I would unregister the receiver before you call finish on your activity though. – zgc7009 Aug 21 '14 at 02:31