-1

I have hybrid app where every page is loaded inside a WebPageActivity(webView). I don't want to create a new activity for every screen but create multiple instances of same activity for each hybrid html page.

Following is the requirement,

Navigation Stack:

A -> B -> D -> E

Here all activities in stack are of type WebPageActivity and every instance is drawing different html.

When user clicks on some button on activity 'E', then it should bring existing 'B' to foreground and clear top, resulting in following stack,

A-> B

Summary:

All the activities in the stack are of same type but having different views and it is required to go back to some activity in stack with clear top.

Available data:

  1. Every activity holds property identifying the name of html file.
  2. Whenever I want to back to activity in the stack, I know the name of html that will be present in that activity.
comrade
  • 4,590
  • 5
  • 33
  • 48
JTeam
  • 1,455
  • 1
  • 11
  • 16
  • And can you just use one ativity without starting new ones and just replace the content? – miva2 Jul 13 '16 at 12:31
  • If i use only one activity then, I would require to maintain a custom back stack for history. – JTeam Jul 17 '16 at 17:44

1 Answers1

1

You should use a Fragment and not make a new instance of the same activity just for displaying a different html. Android has Fragments for the very same use-case as yours.

Also, creating multiple activity instances increases your app's memory footprint. Using Fragments you can easily remove any Fragment from the Fragment back stack by using its unique fragment tag (which you specify while adding it to the activity).

In order to achieve it by making multiple activity instances, make use of the flag CLEAR_TOP in the intent for activity instance 'B'. This should clear all instance on top of B.

Hope this helps

Siddharth2092
  • 377
  • 3
  • 15
  • Yes fragment should do the job, but in the current context to avoid a large change, I was trying to do it with activity but then I do not think it can be done without hacky work with activity. I cannot use CLEAT_TOP, as I want to go back to activity instance in stack and then clear all activities above it, and these are different instance but they are of same type. – JTeam Jul 17 '16 at 17:46