0

My Activity stack is like this:

MainActivity > HomeActivity > MapFragment

When I leave my app from MapFragment in development mode, I can enter the app again and my onResume() method will be called. So I can continue where I left off.

When I leave my app from MapFragment in production mode (installing the app from Google Play), and then enter the app, onCreate() will be called instead. The fragment therefore loses its state and onViewCreated of the fragment will be called again and ruin the UI.

Why is this behaviour different in production mode? How can I simply resume my fragment in onResume() when returning to it like in development mode?

My code is like this:

MainActivity

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
    val intent = Intent(this, HomeActivity::class.java)
    startActivity(intent)
    return finish()
}

HomeActivity

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_home)

MapFragment

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    initialiseFcmToken()
    initialiseProfileFragment()
    initialiseUser()
}

override fun onResume() {
    super.onResume()

PS: When I re-enter the app via "Recent apps" no my phone, it successfully resumes the fragment. But if I re-enter the app by clicking the app icon in my phone's launcher, it will call onCreate() instead.

Zorgan
  • 8,227
  • 23
  • 106
  • 207
  • Have a look at [This thread](https://stackoverflow.com/questions/19545889/app-restarts-rather-than-resumes) and [This One](https://stackoverflow.com/questions/17545924/android-app-restarts-when-opened-by-clicking-app-icon) or Similar .. – ADM Mar 14 '20 at 07:15
  • Thankyou this https://stackoverflow.com/a/23220151/6733153 solved the issue. – Zorgan Mar 14 '20 at 08:24

0 Answers0