Home > database >  My app doesn't resume from the current activity but restart from the launcher activity every ti
My app doesn't resume from the current activity but restart from the launcher activity every ti

Time:04-19

I hope you're doing great. Please, I need help for an issue. I'm working on an app that has three activities : StartActivity, MainActivity and SettingActivity. The StartActvity is the launcher activity and the MainActivity has NavHostFragment with four fragments. When I launch the app for the first time, it navigates to the MainActivity after some delay; that is one of expected behaviors. Now when, the app navigate to MainActivity, it doesn't resume from it's current state in MainActivity when it's put in background. It restarts from StartActivity every time I put it in Background after interacting with fragments and all the actions are lost. For example, if I start searching something with SearchView in a fragment and put the app in background, it loses what I was doing and restart from StartActivity instead of resuming from MainActivity. I've tried some solutions but it doesn't work.

Here are my activities codes :

StartActivity

@Suppress("DEPRECATION")
class StartActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_start)

        if (!isTaskRoot){
            finish()
            return
        }

        Handler().postDelayed({
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }, 1800)

    }

}

Here, I first used finish() in the intent and as it didn't work, I added the code that is in if statement

MainActivity

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    private lateinit var navController: NavController
    private lateinit var appBarConfiguration: AppBarConfiguration

    private lateinit var binding : ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navHostFragment = supportFragmentManager
            .findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController

        appBarConfiguration = AppBarConfiguration(navController.graph, binding.drawerLayout)

        NavigationUI.setupActionBarWithNavController(this, navController, binding.drawerLayout)
        NavigationUI.setupWithNavController(binding.navView, navController)

    }

    override fun onSupportNavigateUp(): Boolean {
        return NavigationUI.navigateUp(navController, appBarConfiguration)
    }

}

My manifest content

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="eg.esperantgada.dailytodo">

    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

    <application
        android:name=".application.TodoApplication"
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.DailyToDo"
        tools:ignore="DataExtractionRules">

        <activity
            android:name=".StartActivity"
            android:configChanges="orientation|screenSize"
            android:theme="@style/Theme.AppCompat.NoActionBar"
            android:launchMode="singleTop"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <activity
            android:name=".MainActivity"
            android:noHistory="true"
            android:windowSoftInputMode="adjustResize"
            android:configChanges="orientation|screenSize"/>

        <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings" />

        <receiver android:name=".broadcastreceiver.TodoAlarmReceiver" />
    </application>

</manifest>

If anyone can help me to fix it, I'll be very glad. Thanks in advance.

CodePudding user response:

If you delete android:launchMode="singleTop" from your StartActivity tag in the manifest file it should be all done.

You can read more about singletop in this stackOverFlow solution https://stackoverflow.com/a/3283118/6565331

CodePudding user response:

I think the issue comes from android:noHistory="true". I've added it when fixing another issue and forgotten to remove it. Now after removing it, the problem is fixed.

  • Related