0

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!

Josef M
  • 412
  • 2
  • 5
  • 20

1 Answers1

1

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.

  • What's the point of an Activity nowadays then, is it completely deprecated in favour of using Fragments? – Martin Melka Jun 09 '22 at 15:39
  • It's dependent on the architecture that you choose to employ, for a simple application activities is the way to go, as its much easier to implement and manage. – Daniel Isaac Jun 09 '22 at 15:56
  • So for more complex applications we should focus on the use of fragments more than activities then ? – Josef M Jun 09 '22 at 16:15
  • You can choose either, you can have a complex application with Activities, or Fragments. None is significantly better than the other. For fragment based apps you can check out https://developer.android.com/guide/navigation/navigation-migrate – Daniel Isaac Jun 10 '22 at 01:40
  • oh I see, that's interesting to know! It seems though that the general practice is, to use less activities and more fragments since its seems expensive to create/pause/destroy activities compared to fragments. Atleast since they introduced Jetpack! Found a similar topic on here as well, definately worth checking out! https://stackoverflow.com/questions/20306091/dilemma-when-to-use-fragments-vs-activities – Josef M Jun 10 '22 at 07:48