在 Android 中显示一次片段?科特林

Show fragment once in Android? Kotlin

我想在我的应用程序中实现一个功能,当应用程序首次启动时,片段将只显示一次。 遇到了问题。启动应用程序时显示错误。 我正在寻求社区的帮助来解决这个问题。 下面我附上了代码片段中的代码以及启动应用程序时出现的错误

登录片段


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(this@LoginFragment.context, ListFragment::class.java)
            startActivity(intent)

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

错误

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

ListFragment 是片段而不是 activity,不能使用 startActivity() 启动片段。

您可以像这样从片段事务开始您的列表片段

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(); 

如果您使用的是导航图,则可以使用导航控制器和片段方向

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