Search code examples
kotlinandroid-fragmentsandroid-activity

Activities vs Fragments for apps


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!


Solution

  • 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.