1

I'm creating an app and I'm using a library called "SlidingActivity". [Github Link]

I actually have two activities. One being the main activity of the app and the other one extending SlidingActivity. So when the SlidingActivity is opened, it's still possible to see the main activity in the background (see the images on the Github page).

Is it possible to edit the content/layout of the main activity when the SlidingActivity is opened? I tried using getParent() but it's returning null.

Edit: As @Hamza Hathoute suggested I've tried overriding onPause() and onDestroy(). I've seen that onPause() is called each time the SlidingActivity is opened.

Thanks in advance. I'm new to StackOverflow so if there is anything I've done wrong please tell me!

Neïl Rahmouni
  • 326
  • 2
  • 10

3 Answers3

2

The issue you are facing is one of communication. That is, you want the SlidingActivity to tell the MainActivity that it should change its content. While there are a few approaches to this issue the simplest might be to use the LocalBroadcastManager to send a broadcast.

Edit:

An activity that is not in the foreground can be killed by the OS in low memory situations. So you should register your receiver in onCreate and unregister in onDestroy. It is therefore possible that you might miss a broadcast (if your activity was destroyed when the broadcast was sent).

If you want to cover this case then unless you want to deal with persistence (shared prefs, db) then you should probably use the startActivityForResult option mentioned in another answer. The downside of that approach is that the changes to MainActivity aren't immediate. So if the sliding activity isn't full screen then you won't see changes in the MainActivity.

MidasLefko
  • 4,499
  • 1
  • 31
  • 44
  • Oooh I see. I'll try that ! But is really the other activity still running or is the activity in the background just a clone of the content of the previous one? Thanks a lot ! – Neïl Rahmouni Jul 29 '18 at 21:01
  • Added more info to my answer – MidasLefko Jul 30 '18 at 07:10
  • @Neïl Have you considered using fragment instead of a new activity? Then you can just use an interface to communicate and update your activity – MRah Jul 30 '18 at 17:13
  • @MRahThat's a good idea, but I'm kind of new to Android so I don't fully understand the library's code yet. Once i'll be able to fully understand how it works I may try to make it a fragment instead of an Activity. – Neïl Rahmouni Jul 31 '18 at 12:26
  • @Neïl I'm happy that it worked. And welcome to StackOverflow! – MidasLefko Jul 31 '18 at 13:52
0

If you want to show the main activity in the background, you can use a transparent background for the sliding activity.

So, you should pass whatever params you needed from the main activity (not the activity object) using intent, use that to populate your sliding activity (designed with a transparent background).

In the sliding activity, you can save the desired params to modify the main activity when you come back to the main activity.

If your sliding activity always returns some results to the main activity, you can use startActivityForResult,

See here for implemantation: How to manage `startActivityForResult` on Android?

MRah
  • 940
  • 6
  • 15
0

is really the other activity still running or is the activity in the background just a clone of the content of the previous one?

You may check this by overriding onPause and onDestroy and adding a Log Message. If it doesn't display any message then you definitely can edit it, just pay attention to the performance.

Hamza Hathoute
  • 438
  • 3
  • 12
  • That's a good idea! If it's still running i'll follow the answer from @MidasLefko otherwise i'll try to find the view where the content is cloned. – Neïl Rahmouni Jul 30 '18 at 10:13
  • so did you manage to get it working? I'm interested in implementing this in my future apps. – Hamza Hathoute Aug 01 '18 at 12:18
  • Yup it works perfectly! I've edited my question ~5 days ago to share my progress concerning your answer. As the Main Activity is just paused I then followed @MidasLefko answer and It works perfectly using LocalBroadcastManager ! – Neïl Rahmouni Aug 04 '18 at 13:38
  • Nice to hear this, I'll definitely implement the SlidingActivity in my future apps. – Hamza Hathoute Aug 04 '18 at 18:13