简体   繁体   中英

Android activity with two fragments

So I have to create a login activity + fragments (login, register). I want the home screen to be two buttons (login+register) which I've already created in my main activity's xml file.

However I'm struggling to understand how I"m supposed to add two fragments to my activity (one login activity, which would bring me to first fragment with a loginscreen and one register activity which would bring me to the second fragment with a registerscreen).

So I created a new project in android studio with a blank activity. Then I right clicked the res/layout folder and pressed create new blank fragment. However, this fragment doesn't get added to the folder where my MainAcivity.java file is in, a new folder called layout got created and it got added there. How do I add these fragments to my main activity? Is it even possible to make the main acitivity get replaced by a fragment within that activity (so my homepage gets hidden when i click one button and then the fragment gets shown)? Or how should I go about this?

I'm sorry if this seems confusing, it is for me to. Any help would be much appreciated.

I want the home screen to be two buttons (login+register) which I've already created in my main activity's xml file

Okay, great!

I'm struggling to understand how I"m supposed to add two fragments to my activity (one login activity, which would bring me to first fragment with a loginscreen and one register activity which would bring me to the second fragment with a registerscreen).

Do you want a LoginFragment and RegisterFragment ? That makes more sense than two Activitys. You additionally would have a HomePageFragment with the 2 buttons.

this fragment doesn't get added to the folder where my MainAcivity.java file is in, a new folder called layout got created and it got added there

It's not clear how that happened, but a Fragment, just like an Activity has a XML layout and a Java class. If you got a layout package added in the Java path, then you don't really need it and can move the java files next to one another.

How do I add these fragments to my main activity?

Use FragmentTransaction , for example, from an AppCompatActivity .

LoginFragment loginFrag = new LoginFragment();

// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
        .add(R.id.fragment_container, loginFrag).commit();

Is it even possible to make the main acitivity get replaced by a fragment within that activity (so my homepage gets hidden when i click one button and then the fragment gets shown)?

You don't replace the Activity, you "transition" / "replace" Fragments that are displayed within a parent Activity.

For more details, see Building a Flexible UI

Try this:

  • First, when you created a project, the generated project gives you a layout for your activity and the activity class itself.
  • Secondly, if you want to add a new fragment, you can add a Fragment class to your project (sample folder as your activity) and also create a layout xml file for that fragment.
  • Now, in your activity, before you load the corresponding fragment, you will need two buttons - one for login and another for Register.
  • Important thing to remember here is to have a FrameLayout inside your MainActivity's layout xml that will hold your fragment.
  • When a user selects Login, you create an instance of the LoginFragment, use the FragmentManager to commit the transaction.
  • When a user selects register, you create an instance of the RegisterFragment, use the FragmentManager to commit the transaction.

Since you will be using the FrameLayout, you will be replacing the second fragment if the first one is already there!

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM