0

I'm developing an app which consists of two library projects. Both library projects are used by the actual app.

The first library project can be seen as the main library project as it contains the main menu in form of a dashboard for the whole app. From one entry in the dashboard the user can start an activity which is part of the second library project.

All activities and any further once that can be started from this activity are also part of the second library project. So the second library project doesn't know anything about the first one.

At the end of the action flow the user should return to the dashboard activity by clicking a single button. So I need to clear the whole activity stack excluding the first activity (dashboard). Normally I would do this by starting the Dashboard activity with the flag FLAG_ACTIVITY_CLEAR_TOP. Also using FLAG_ACTIVITY_NO_HISTORY won't be an option as I need the history.

But as I mentioned before none of the components of the second library project should not know anything about the main library project. So calling the Dashboard activity with the flag is no solution.

So I'm looking for a way to finish all activities of the second library project but not the dashboard activity from the main library project.

Flo
  • 27,355
  • 15
  • 87
  • 125
  • I know you said 'without calling it explicitly', but if what you intent is not to create multiple instances of the dashboard... make the dashboard `singleInstance` and call it explicitly. This way it won't be created again, and will be brought to the top. You have your history as well. – Aswin Kumar Sep 24 '12 at 13:46
  • No that's not the real problem and therefore no solution. The real problem is that I want to use the second library project also in another app and therefore I cannot reference anything outside of it. That's why I cannot call the Dashboard activity explicitly. – Flo Sep 24 '12 at 13:53

1 Answers1

2

When your dashboard calls the second activity, bundle the class name(as a string) in an intent. In your second activity, you can use that class name to create the intent to return.

Use MyActivity.getClass().getName() to form the string.

Use Class.forName(className) to get the class back.

Edit: You may also need to grab the full package name for forName() to work: .getClass().getPackage().getName()

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • 1
    +1 for this. However, instead of passing the class name in a string extra to the second library project, why not just pass an `Intent` from the dashboard to the second library project. The second library project can then use this Intent to return to the dashboard. In this case, the dashboard can specify whatever flags it wants (for example: CLEAR_TOP) in addition to have the package and class name properly set. – David Wasser Sep 24 '12 at 14:03
  • Ah, that would be clearer, wouldn't it? I forgot you could bundle Intents inside Intents. – Geobits Sep 24 '12 at 14:09