-2

I have a MainActivity in my Android Application where I am showing a list of items in a RecyclerView.

Each Item can have multiple sub items, and sub items can have its own child items too (like a tree). I want to show the same activity for main and sub items, but with an Back Arrow on ActionBar.

Is it possible to reuse MainActivity?

Yesudass Moses
  • 1,841
  • 3
  • 27
  • 63

4 Answers4

1

Yes, it is possible to reuse MainActivity. Each time you call startActivity without special launchMode or flags, a new instance of MainActivity will be created and it is considered an absolutely new one.

Cao Minh Vu
  • 1,900
  • 1
  • 16
  • 21
1

yes, you can do this using fragments. Instead of showing lists in MainActivity take a fragment container in MainActivity and create fragments where you can show your lists and subitems according to your requirement.

By checking fragment instance in MainActivity, you can show and hide backarrow of actionbar.

Check this

Jyot
  • 540
  • 5
  • 17
1

I have encountered a similar problem while working on a File Manager application. So What I did is created a FolderNavigator stack that helped me in keeping the track of users current position using which I showed breadcrumbs at the top. And I hooked my fragment with this navigator so as soon as the peek of stack changes my folder gets notified and it will load the data of the peek of the stack. When I press back I will just pop the stack and as my fragment is already hooked with the peek of stack it will show the respective data.

hiten pannu
  • 186
  • 1
  • 6
0

You can use fragments to fulfill your requirements.

From this SO answer:

Fragments are more of a UI benefit in my opinion. It's convenient for the user sometimes to see two different views of two different classes on the same screen.

For more about fragments, read this document.

Pang
  • 9,564
  • 146
  • 81
  • 122