活动与应用程序的片段

发布于 2025-02-05 19:49:02 字数 248 浏览 1 评论 0原文

我一直在试图解决这个问题。我看到很多开发人员和YouTuber(教程)说,我们应该使用尽可能少的活动,以便进行更快,更高效,更少的资源重型代码/应用程序。我想知道是否有一种方法可以仅使用MainAttivity登录和注册来登录和注册,并结合片段进行导航。

我们是否需要至少2个或更多的活动来处理该过程(登录注册)?

示例:1个登录活动和1个活动的活动。

感谢并欢迎有关此主题的任何答案!

I have been trying to figure this out for a bit now. I see a lot of developers and youtubers (tutorials) saying that we should use as little activities as possible in order for a faster, more efficient and less resource heavy code/app. I was wondering if there is a way to create a Log-in and Sign-up using only the MainActivity for both Log-in and Sign-Up in combination with fragments for navigation between them.

Or

Do we need atleast 2 or more activities to handle that process ( Log-in & Sign-Up )?

Example: 1 activity for log-in and 1 activity for sign-up.

Appreciate and welcome any answers regarding this topic!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅渡 2025-02-12 19:49:02

从理论上讲,您可以将整个应用程序运行在一个活动上,并在所有页面上使用片段。

每个片段在活动中都有自己的生命周期。

MainAttivity看起来像是这种

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        loadFragment(2)
    }

    public fun loadFragment(page: Int){
        if(page == 1){
            // load login
            val manager = supportFragmentManager.beginTransaction()
            manager.replace(R.id.fragment_holder, LoginFragment()).commit()
        }else{
            // load register
            val manager = supportFragmentManager.beginTransaction()
            manager.replace(R.id.fragment_holder, LoginFragment()).commit()
        }
    }
}

登录范围看起来像是这种

class LoginFragment : Fragment() {

    lateinit var myButton: Button

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

    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)
        myButton = view.findViewById(R.id.my_button)

        myButton.apply {
            setOnClickListener { toRegister() }
        }
        return view;
    }

    fun toRegister(){
        // replace the fragment in main activity with register fragment
        (requireActivity() as MainActivity).loadFragment(1)
    }
}

登记板看起来像是这样

class RegisterFragment : Fragment() {

    lateinit var mButton: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_register, container, false)
        mButton = view.findViewById(R.id.my_button)
        mButton.apply {
            setOnClickListener { toLogin() }
        }
        return view;
    }

    fun toLogin(){
        // replacing the fragment in the main activity with the login fragment
        (requireActivity() as MainActivity).loadFragment(1)
    }
}

基本上,我们通过调用loadFragment替换了活动中显示的片段。
该逻辑可以应用于必要的尽可能多的片段。

Theoretically, you could have your entire application run on a single Activity and use Fragments for all of the pages.

Each fragment has its own lifecycle within the activity.

MainActivity can look like this

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        loadFragment(2)
    }

    public fun loadFragment(page: Int){
        if(page == 1){
            // load login
            val manager = supportFragmentManager.beginTransaction()
            manager.replace(R.id.fragment_holder, LoginFragment()).commit()
        }else{
            // load register
            val manager = supportFragmentManager.beginTransaction()
            manager.replace(R.id.fragment_holder, LoginFragment()).commit()
        }
    }
}

LoginFragment can look like this

class LoginFragment : Fragment() {

    lateinit var myButton: Button

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

    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)
        myButton = view.findViewById(R.id.my_button)

        myButton.apply {
            setOnClickListener { toRegister() }
        }
        return view;
    }

    fun toRegister(){
        // replace the fragment in main activity with register fragment
        (requireActivity() as MainActivity).loadFragment(1)
    }
}

RegisterFragment can look like this

class RegisterFragment : Fragment() {

    lateinit var mButton: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_register, container, false)
        mButton = view.findViewById(R.id.my_button)
        mButton.apply {
            setOnClickListener { toLogin() }
        }
        return view;
    }

    fun toLogin(){
        // replacing the fragment in the main activity with the login fragment
        (requireActivity() as MainActivity).loadFragment(1)
    }
}

Basically, we replace the fragment displayed in the activity by calling loadfragment.
This logic can be applied to as many fragments as is necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文