Home > other >  Show fragment once in Android? Kotlin
Show fragment once in Android? Kotlin

Time:12-11

I want to implement a function in my application with the help of which, when the application is first launched, the fragment will be shown only once. Faced a problem. An error is shown when launching the application. I am asking for help from the community to solve this problem. Below I attached the code from the snippet and the error that appears when launching the application

Login Fragment


import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.skreep.subeeapp.R
import android.content.Intent
import android.content.SharedPreferences
import androidx.navigation.fragment.findNavController
import com.skreep.subeeapp.MainActivity
import com.skreep.subeeapp.fragments.list.ListFragment
import kotlinx.android.synthetic.main.fragment_login.*
import java.nio.file.Files.move


private const val SHARED_PREFS_NAME = "shared_prefs_name"

class LoginFragment : Fragment() {


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_login, container, false)
        return view
//        button_continue?.setOnClickListener {
//            findNavController().navigate(R.id.action_loginFragment2_to_listFragment)


    }


    override fun onResume() {
        super.onResume()
        val sharedPref = activity?.getPreferences(
            Context.MODE_PRIVATE
        ) ?: return
        val first = sharedPref.getString(SHARED_PREFS_NAME, "")

        if (first.equals("yes")) {
            val intent = Intent([email protected], ListFragment::class.java)
            startActivity(intent)

        } else {
            val editor = sharedPref.edit();
            editor.putString(SHARED_PREFS_NAME, "yes");
            editor.apply();
        }
    }
}

Error

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.skreep.subeeapp, PID: 16473
    java.lang.RuntimeException: Unable to resume activity {com.skreep.subeeapp/com.skreep.subeeapp.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.skreep.subeeapp/com.skreep.subeeapp.fragments.list.ListFragment}; have you declared this activity in your AndroidManifest.xml?
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4270)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4302)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7560)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.skreep.subeeapp/com.skreep.subeeapp.fragments.list.ListFragment}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2064)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1720)
        at android.app.Activity.startActivityForResult(Activity.java:5260)
        at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:597)
        at android.app.Activity.startActivityForResult(Activity.java:5218)
        at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:583)
        at android.app.Activity.startActivity(Activity.java:5589)
        at androidx.core.content.ContextCompat$Api16Impl.startActivity(ContextCompat.java:828)
        at androidx.core.content.ContextCompat.startActivity(ContextCompat.java:276)
        at androidx.fragment.app.FragmentHostCallback.onStartActivityFromFragment(FragmentHostCallback.java:166)
        at androidx.fragment.app.Fragment.startActivity(Fragment.java:1377)
        at androidx.fragment.app.Fragment.startActivity(Fragment.java:1365)
        at com.skreep.subeeapp.fragments.login.LoginFragment.onResume(LoginFragment.kt:47)
        at androidx.fragment.app.Fragment.performResume(Fragment.java:3039)
        at androidx.fragment.app.FragmentStateManager.resume(FragmentStateManager.java:607)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:306)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
        at androidx.fragment.app.FragmentManager.dispatchResume(FragmentManager.java:3086)
        at androidx.fragment.app.Fragment.performResume(Fragment.java:3048)
        at androidx.fragment.app.FragmentStateManager.resume(FragmentStateManager.java:607)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:306)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
        at androidx.fragment.app.FragmentManager.dispatchResume(FragmentManager.java:3086)
        at androidx.fragment.app.FragmentController.dispatchResume(FragmentController.java:273)
        at androidx.fragment.app.FragmentActivity.onResumeFragments(FragmentActivity.java:458)
        at androidx.fragment.app.FragmentActivity.onPostResume(FragmentActivity.java:447)
E/AndroidRuntime:     at androidx.appcompat.app.AppCompatActivity.onPostResume(AppCompatActivity.java:240)
        at android.app.Activity.performResume(Activity.java:8081)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4260)
            ... 11 more
I/skreep.subeeap: ProcessProfilingInfo new_methods=1175 is saved saved_to_disk=1 resolve_classes_delay=8000

CodePudding user response:

ListFragment is a fragment and not an activity, you cannot start a fragment with startActivity().

You can start your list fragment from a Fragment transaction like this

Fragment someFragment = new ListFragment(); 
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, someFragment ); // give your fragment container id in first parameter
transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
transaction.commit(); 

If you're using a Navigation graph, you can do it using the navigation controller and Fragment directions

https://developer.android.com/guide/navigation/navigation-navigate

  • Related