0

I have 2 activities. Main Activity A & Activity B I do not want Activity A to destroy. I am starting Activity B in a new task.

public static void startActivity(Class<?> startClass) {
    Intent intent = new Intent(Constants.getActivity(), startClass);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Constants.getActivity().startActivity(intent);
}`
  • Constants.getActivity() returns the Context on current activity
  • startClass is the either activity "A" or activity "B"

Thing is they create/destroy the activities and they leak. Am I doing it wrong? How can I start activity "B" from activity "A" and vice versa keep them both in background when I dont need them.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
Ionut Bogdan
  • 249
  • 1
  • 3
  • 15

3 Answers3

0

When you use tasks, cleanup is very important. You need to cleanup all tasks in the activity. readthis

If the activity does not have a lot of crazy initialization, just use finish and onCreates. Else be aware that onResume will be called often as you switch between activity's. Cleanup will be crucial here. If you dont cleanup, its possible one of your activities (with dead object reference from the other cleaned up activity) may come back up from the activity stack and throw exceptions. Its very difficult to debug this kinda exception.

Community
  • 1
  • 1
Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • Hello. Thanks for editing my post. The idea behind my post is that im working on a embedded system that runs Android. There is no "BACK" button, the change between activities is done by a GPIO. Weather the GPIO is 0 or 1, activities change. In my application i have another class that reads the GPIOs values and according to their values it executes certain functions. One of the function i posted in my original post. What i would like to know is weather is possible to keep both activities "alive" and just toggle between background/foreground. – Ionut Bogdan Oct 19 '12 at 07:54
  • No you cannot. I give you two likely options: 1) you might use fragments to change the UI and keep the activity, and the activity receive callbacks from the GPIO. 2) Create a singleton that holds reference to a listener for GPIO changes. Each activity implements the listener interface and onPause/onResume register/unregister itself as the new listener. – Budius Oct 19 '12 at 08:12
0

First of all, what are you trying to do? You should always separate things you want to do in the background from your UI. Think of your Activities are simply a container to show the UI, everything else can be stored and restored from persistent storage or savedinstance bundles.

It is very crucial that you understand the difference between Activity lifecycle and Service lifecycle here.

I'm going to refer to my answer from another question here:

Your app will be in the background, onResume and onPause will both be called, provided that the OS have enough memory to keep the new app and all the old apps.

  • If you have a long running process that you need while the user not looking at it, use a service.

  • If you need the user to return the app in the same state, you need to do the work in onResume and onPause to save the state information and initialize your UI again when the user comes back. If you are really worried that it can get killed (well, it shouldn't lose the bundle I think), you can store them in SharePreferences for your app.

  • If you want to know when the app returns from that specific share intent, use startActivityForResult

Edison
  • 5,961
  • 4
  • 23
  • 39
0

You cannot keep an activity "alive" as you said. When an activity is paused, Android system is always able to claim its memory and unload it, at any time.

But if you want to switch from A to B, then maybe A and B can be different views inside a single activity. Maybe you'll want to have a look at http://developer.android.com/reference/android/widget/ViewFlipper.html

Orabîg
  • 11,718
  • 6
  • 38
  • 58