How to correctly put my activities in Tutorial´s Bottom Navigation?
I am new to programming. I have 4 working activities with 4 xml layouts. I also copied this simple example fragment (I think I need 4 of those for my 4 activites) from a tutorial:
public class SelectFriends extends Fragment {
public SelectFriends () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_SelectFriends , container, false);
}
}
This is the relevant part from MainActivity:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_item1:
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(R.id.container, SelectFriends).commit();
return true;
case R.id.navigation_item2:
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(R.id.container, StartFood).commit();
return true;
[...]
}
return false;
}
I can start my activitie´s selectFriends.xml when I replace fragment_SelectFriends in @Override public View onCreateView
with ID of selectFriends.xml . The problem is I don´t know where to put the corresponding Activity, so the selectFriends.xml shows up correctly with BottomNavigation but there is no interaction possible, of course. What is best practice? Internet is confusing me: am I understanding the use of fragments false? I don´t even understand why I should make fragments with BottomNavigation when the things I want to show in the different BottomNavigation displays are completely different from each other. Thank you
android bottomnavigationview
add a comment |
I am new to programming. I have 4 working activities with 4 xml layouts. I also copied this simple example fragment (I think I need 4 of those for my 4 activites) from a tutorial:
public class SelectFriends extends Fragment {
public SelectFriends () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_SelectFriends , container, false);
}
}
This is the relevant part from MainActivity:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_item1:
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(R.id.container, SelectFriends).commit();
return true;
case R.id.navigation_item2:
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(R.id.container, StartFood).commit();
return true;
[...]
}
return false;
}
I can start my activitie´s selectFriends.xml when I replace fragment_SelectFriends in @Override public View onCreateView
with ID of selectFriends.xml . The problem is I don´t know where to put the corresponding Activity, so the selectFriends.xml shows up correctly with BottomNavigation but there is no interaction possible, of course. What is best practice? Internet is confusing me: am I understanding the use of fragments false? I don´t even understand why I should make fragments with BottomNavigation when the things I want to show in the different BottomNavigation displays are completely different from each other. Thank you
android bottomnavigationview
I've noticed that this is at least the third time that you've posted this same question (stackoverflow.com/q/53219136, stackoverflow.com/q/53234243). Firstly, please don't repeat questions. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working, will bump it to the top of the active queue. Secondly, it seems that you might have a fundamental misunderstanding of the nature of Activities and Fragments.
– Mike M.
Nov 10 at 21:09
Fragments do not have a one-to-one correspondence to Activities, and you do not need a separateActivity
class for eachFragment
class. A singleActivity
can host and manipulate multipleFragment
s, which seems to be what you're looking to do. When you say that you have "4 working activities with 4 xml layouts", it seems that you actually want fourFragment
s hosted within a singleActivity
. If that's the case, you simply need to transact the pertinentFragment
s within that singleActivity
.
– Mike M.
Nov 10 at 21:09
@Mike M. Thank you for replying, it seems you understood my issue. My question is how do I transact the pertinent Fragments within that single Activity? By putting all 4 Activities somewhere into public class SelectFriends extends Fragment? Isn´t it bad practice because of battery drain: Everytime the App is opened that big activity code must be loaded even when the user intented to use only a litte part (1 activity) of the App...
– Tony
Nov 10 at 22:10
As @Mike M. stated above you need one Activity which will hold your 4 other fragmens. After your activity is created it will load the first fragment. Then if you press a button in the navigation the activity will replace the current fragment with another one and so on...
– Skemelio
Nov 11 at 2:13
@Skemelio If I understand you right, I have to put the two pieces of code that I posted above into one big class, and the corresponding Activity where "// Required empty public constructor" is falsely written. Then I do the same with the other 3 Activities: I write a new class "StartFood" into that one big class. Can you give me a short example please, if I am wrong? Thank you very much!
– Tony
Nov 11 at 19:06
add a comment |
I am new to programming. I have 4 working activities with 4 xml layouts. I also copied this simple example fragment (I think I need 4 of those for my 4 activites) from a tutorial:
public class SelectFriends extends Fragment {
public SelectFriends () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_SelectFriends , container, false);
}
}
This is the relevant part from MainActivity:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_item1:
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(R.id.container, SelectFriends).commit();
return true;
case R.id.navigation_item2:
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(R.id.container, StartFood).commit();
return true;
[...]
}
return false;
}
I can start my activitie´s selectFriends.xml when I replace fragment_SelectFriends in @Override public View onCreateView
with ID of selectFriends.xml . The problem is I don´t know where to put the corresponding Activity, so the selectFriends.xml shows up correctly with BottomNavigation but there is no interaction possible, of course. What is best practice? Internet is confusing me: am I understanding the use of fragments false? I don´t even understand why I should make fragments with BottomNavigation when the things I want to show in the different BottomNavigation displays are completely different from each other. Thank you
android bottomnavigationview
I am new to programming. I have 4 working activities with 4 xml layouts. I also copied this simple example fragment (I think I need 4 of those for my 4 activites) from a tutorial:
public class SelectFriends extends Fragment {
public SelectFriends () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_SelectFriends , container, false);
}
}
This is the relevant part from MainActivity:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_item1:
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(R.id.container, SelectFriends).commit();
return true;
case R.id.navigation_item2:
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.fade_out).replace(R.id.container, StartFood).commit();
return true;
[...]
}
return false;
}
I can start my activitie´s selectFriends.xml when I replace fragment_SelectFriends in @Override public View onCreateView
with ID of selectFriends.xml . The problem is I don´t know where to put the corresponding Activity, so the selectFriends.xml shows up correctly with BottomNavigation but there is no interaction possible, of course. What is best practice? Internet is confusing me: am I understanding the use of fragments false? I don´t even understand why I should make fragments with BottomNavigation when the things I want to show in the different BottomNavigation displays are completely different from each other. Thank you
android bottomnavigationview
android bottomnavigationview
asked Nov 10 at 20:49
Tony
64
64
I've noticed that this is at least the third time that you've posted this same question (stackoverflow.com/q/53219136, stackoverflow.com/q/53234243). Firstly, please don't repeat questions. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working, will bump it to the top of the active queue. Secondly, it seems that you might have a fundamental misunderstanding of the nature of Activities and Fragments.
– Mike M.
Nov 10 at 21:09
Fragments do not have a one-to-one correspondence to Activities, and you do not need a separateActivity
class for eachFragment
class. A singleActivity
can host and manipulate multipleFragment
s, which seems to be what you're looking to do. When you say that you have "4 working activities with 4 xml layouts", it seems that you actually want fourFragment
s hosted within a singleActivity
. If that's the case, you simply need to transact the pertinentFragment
s within that singleActivity
.
– Mike M.
Nov 10 at 21:09
@Mike M. Thank you for replying, it seems you understood my issue. My question is how do I transact the pertinent Fragments within that single Activity? By putting all 4 Activities somewhere into public class SelectFriends extends Fragment? Isn´t it bad practice because of battery drain: Everytime the App is opened that big activity code must be loaded even when the user intented to use only a litte part (1 activity) of the App...
– Tony
Nov 10 at 22:10
As @Mike M. stated above you need one Activity which will hold your 4 other fragmens. After your activity is created it will load the first fragment. Then if you press a button in the navigation the activity will replace the current fragment with another one and so on...
– Skemelio
Nov 11 at 2:13
@Skemelio If I understand you right, I have to put the two pieces of code that I posted above into one big class, and the corresponding Activity where "// Required empty public constructor" is falsely written. Then I do the same with the other 3 Activities: I write a new class "StartFood" into that one big class. Can you give me a short example please, if I am wrong? Thank you very much!
– Tony
Nov 11 at 19:06
add a comment |
I've noticed that this is at least the third time that you've posted this same question (stackoverflow.com/q/53219136, stackoverflow.com/q/53234243). Firstly, please don't repeat questions. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working, will bump it to the top of the active queue. Secondly, it seems that you might have a fundamental misunderstanding of the nature of Activities and Fragments.
– Mike M.
Nov 10 at 21:09
Fragments do not have a one-to-one correspondence to Activities, and you do not need a separateActivity
class for eachFragment
class. A singleActivity
can host and manipulate multipleFragment
s, which seems to be what you're looking to do. When you say that you have "4 working activities with 4 xml layouts", it seems that you actually want fourFragment
s hosted within a singleActivity
. If that's the case, you simply need to transact the pertinentFragment
s within that singleActivity
.
– Mike M.
Nov 10 at 21:09
@Mike M. Thank you for replying, it seems you understood my issue. My question is how do I transact the pertinent Fragments within that single Activity? By putting all 4 Activities somewhere into public class SelectFriends extends Fragment? Isn´t it bad practice because of battery drain: Everytime the App is opened that big activity code must be loaded even when the user intented to use only a litte part (1 activity) of the App...
– Tony
Nov 10 at 22:10
As @Mike M. stated above you need one Activity which will hold your 4 other fragmens. After your activity is created it will load the first fragment. Then if you press a button in the navigation the activity will replace the current fragment with another one and so on...
– Skemelio
Nov 11 at 2:13
@Skemelio If I understand you right, I have to put the two pieces of code that I posted above into one big class, and the corresponding Activity where "// Required empty public constructor" is falsely written. Then I do the same with the other 3 Activities: I write a new class "StartFood" into that one big class. Can you give me a short example please, if I am wrong? Thank you very much!
– Tony
Nov 11 at 19:06
I've noticed that this is at least the third time that you've posted this same question (stackoverflow.com/q/53219136, stackoverflow.com/q/53234243). Firstly, please don't repeat questions. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working, will bump it to the top of the active queue. Secondly, it seems that you might have a fundamental misunderstanding of the nature of Activities and Fragments.
– Mike M.
Nov 10 at 21:09
I've noticed that this is at least the third time that you've posted this same question (stackoverflow.com/q/53219136, stackoverflow.com/q/53234243). Firstly, please don't repeat questions. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working, will bump it to the top of the active queue. Secondly, it seems that you might have a fundamental misunderstanding of the nature of Activities and Fragments.
– Mike M.
Nov 10 at 21:09
Fragments do not have a one-to-one correspondence to Activities, and you do not need a separate
Activity
class for each Fragment
class. A single Activity
can host and manipulate multiple Fragment
s, which seems to be what you're looking to do. When you say that you have "4 working activities with 4 xml layouts", it seems that you actually want four Fragment
s hosted within a single Activity
. If that's the case, you simply need to transact the pertinent Fragment
s within that single Activity
.– Mike M.
Nov 10 at 21:09
Fragments do not have a one-to-one correspondence to Activities, and you do not need a separate
Activity
class for each Fragment
class. A single Activity
can host and manipulate multiple Fragment
s, which seems to be what you're looking to do. When you say that you have "4 working activities with 4 xml layouts", it seems that you actually want four Fragment
s hosted within a single Activity
. If that's the case, you simply need to transact the pertinent Fragment
s within that single Activity
.– Mike M.
Nov 10 at 21:09
@Mike M. Thank you for replying, it seems you understood my issue. My question is how do I transact the pertinent Fragments within that single Activity? By putting all 4 Activities somewhere into public class SelectFriends extends Fragment? Isn´t it bad practice because of battery drain: Everytime the App is opened that big activity code must be loaded even when the user intented to use only a litte part (1 activity) of the App...
– Tony
Nov 10 at 22:10
@Mike M. Thank you for replying, it seems you understood my issue. My question is how do I transact the pertinent Fragments within that single Activity? By putting all 4 Activities somewhere into public class SelectFriends extends Fragment? Isn´t it bad practice because of battery drain: Everytime the App is opened that big activity code must be loaded even when the user intented to use only a litte part (1 activity) of the App...
– Tony
Nov 10 at 22:10
As @Mike M. stated above you need one Activity which will hold your 4 other fragmens. After your activity is created it will load the first fragment. Then if you press a button in the navigation the activity will replace the current fragment with another one and so on...
– Skemelio
Nov 11 at 2:13
As @Mike M. stated above you need one Activity which will hold your 4 other fragmens. After your activity is created it will load the first fragment. Then if you press a button in the navigation the activity will replace the current fragment with another one and so on...
– Skemelio
Nov 11 at 2:13
@Skemelio If I understand you right, I have to put the two pieces of code that I posted above into one big class, and the corresponding Activity where "// Required empty public constructor" is falsely written. Then I do the same with the other 3 Activities: I write a new class "StartFood" into that one big class. Can you give me a short example please, if I am wrong? Thank you very much!
– Tony
Nov 11 at 19:06
@Skemelio If I understand you right, I have to put the two pieces of code that I posted above into one big class, and the corresponding Activity where "// Required empty public constructor" is falsely written. Then I do the same with the other 3 Activities: I write a new class "StartFood" into that one big class. Can you give me a short example please, if I am wrong? Thank you very much!
– Tony
Nov 11 at 19:06
add a comment |
1 Answer
1
active
oldest
votes
Let's start from your question:
How to correctly put my activities in Tutorial´s Bottom Navigation?
You will not put your activities in the Bottom Navigation.
You will have one Activity
which will draw the layout of your screen. Secondly you will have a number of Fragment
's which will represent some parts of your screen.
How many Fragment
's? As many as the Bottom Navigation's options.
Take the Youtube app as an example.
The whole screen is an Activity
, Youtube's MainActivity. As you can see the user pressed Subscriptions in the Bottom Navigation so the MainActivity called a SubscriptionsFragment
to draw the subscriptions part of the screen (all the layout except the ActionBar at the top and the Bottom Navigation at the bottom.
If a user selects Home at the Bottom Navigation then the MainActivity
will replace the part of the screen where the SubscriptionsFragment
draw its layout with the HomeFragment
's layout. And the same thing will happen with the other options of Youtube's Bottom Navigation bar.
So to clarify. There is only one activity here. The MainActivity
. Not four. This activity commands 4 fragments to draw the 4 main parts of its screen (the home part with HomeFragment
, the trending part with TrendingFragment
, the subscription part with SubscriptionFragment
and finally the library part with LibraryFragment
.
From your last comment:
I have to put the two pieces of code that I posted above into one big
class?
No you don't have to. It is not necessary to create one file, such as MainActivity.java (where your MainActivity
is defined), and then define the Fragment
's classes inside the same file.
You want to display 4 screens using a bottom navigation, right? Create a file for your activity and four separate files, one per Fragment
.
Example:
Your first file, SelectFriends.java, where SelectFriends fragment is defined, as you've posted above...
public class SelectFriends extends Fragment {
public SelectFriends () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_SelectFriends , container, false);
}
}
Three more separate files like this one, one per fragment.
And lastly your activity, again, as you've posted above.
public class MainActivity extends AppCompatActivity{
@Override
public void onCreate(Bundle savedInstanceState){
// ... some code here
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_item1:
..
// Here the activity figured out that the first item of the bottom navigation
// was clicked, so it calls the support fragment manager to display a fragment
// inside the container view
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
.replace(R.id.container, SelectFriends).commit();
return true;
case R.id.navigation_item2:
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
.replace(R.id.container, StartFood).commit();
return true;
}
return false;
}
}
Edit: Read the Ultimate Guide to Bottom Navigation on Android
thank you for your vast reply. I understand everything you told me, it´s like I assumed before. I also read the tutorial you linked and many more. But where exactly do I put all my Activities that I already created before starting with all that BottomNavigation stuff? I already had like at least 4 activities each one longer than 400 lines (I even used model layer for best practice). So where exactly do I place all that code which includes lines like: public class RestaurantListActivity extends AppCompatActivity { OR FloatingActionButton fab = findViewById(R.id.floatingActionButton);?
– Tony
Nov 12 at 2:34
First of all instead of thank me it would be better if you would upvote the answer if you found it useful or mark it as correct if that's the case.
– Skemelio
Nov 12 at 8:35
Now to your question: You already 4 activities and you want to convert them into 4 fragments. So you need to move the activities code into the fragments. As the docs say If you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.
– Skemelio
Nov 12 at 8:43
I created: Override public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { and put most of my code in it. Your answer was the most helpful to find the solution so I accepted it.
– Tony
Nov 18 at 0:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243271%2fhow-to-correctly-put-my-activities-in-tutorial%25c2%25b4s-bottom-navigation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let's start from your question:
How to correctly put my activities in Tutorial´s Bottom Navigation?
You will not put your activities in the Bottom Navigation.
You will have one Activity
which will draw the layout of your screen. Secondly you will have a number of Fragment
's which will represent some parts of your screen.
How many Fragment
's? As many as the Bottom Navigation's options.
Take the Youtube app as an example.
The whole screen is an Activity
, Youtube's MainActivity. As you can see the user pressed Subscriptions in the Bottom Navigation so the MainActivity called a SubscriptionsFragment
to draw the subscriptions part of the screen (all the layout except the ActionBar at the top and the Bottom Navigation at the bottom.
If a user selects Home at the Bottom Navigation then the MainActivity
will replace the part of the screen where the SubscriptionsFragment
draw its layout with the HomeFragment
's layout. And the same thing will happen with the other options of Youtube's Bottom Navigation bar.
So to clarify. There is only one activity here. The MainActivity
. Not four. This activity commands 4 fragments to draw the 4 main parts of its screen (the home part with HomeFragment
, the trending part with TrendingFragment
, the subscription part with SubscriptionFragment
and finally the library part with LibraryFragment
.
From your last comment:
I have to put the two pieces of code that I posted above into one big
class?
No you don't have to. It is not necessary to create one file, such as MainActivity.java (where your MainActivity
is defined), and then define the Fragment
's classes inside the same file.
You want to display 4 screens using a bottom navigation, right? Create a file for your activity and four separate files, one per Fragment
.
Example:
Your first file, SelectFriends.java, where SelectFriends fragment is defined, as you've posted above...
public class SelectFriends extends Fragment {
public SelectFriends () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_SelectFriends , container, false);
}
}
Three more separate files like this one, one per fragment.
And lastly your activity, again, as you've posted above.
public class MainActivity extends AppCompatActivity{
@Override
public void onCreate(Bundle savedInstanceState){
// ... some code here
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_item1:
..
// Here the activity figured out that the first item of the bottom navigation
// was clicked, so it calls the support fragment manager to display a fragment
// inside the container view
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
.replace(R.id.container, SelectFriends).commit();
return true;
case R.id.navigation_item2:
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
.replace(R.id.container, StartFood).commit();
return true;
}
return false;
}
}
Edit: Read the Ultimate Guide to Bottom Navigation on Android
thank you for your vast reply. I understand everything you told me, it´s like I assumed before. I also read the tutorial you linked and many more. But where exactly do I put all my Activities that I already created before starting with all that BottomNavigation stuff? I already had like at least 4 activities each one longer than 400 lines (I even used model layer for best practice). So where exactly do I place all that code which includes lines like: public class RestaurantListActivity extends AppCompatActivity { OR FloatingActionButton fab = findViewById(R.id.floatingActionButton);?
– Tony
Nov 12 at 2:34
First of all instead of thank me it would be better if you would upvote the answer if you found it useful or mark it as correct if that's the case.
– Skemelio
Nov 12 at 8:35
Now to your question: You already 4 activities and you want to convert them into 4 fragments. So you need to move the activities code into the fragments. As the docs say If you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.
– Skemelio
Nov 12 at 8:43
I created: Override public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { and put most of my code in it. Your answer was the most helpful to find the solution so I accepted it.
– Tony
Nov 18 at 0:01
add a comment |
Let's start from your question:
How to correctly put my activities in Tutorial´s Bottom Navigation?
You will not put your activities in the Bottom Navigation.
You will have one Activity
which will draw the layout of your screen. Secondly you will have a number of Fragment
's which will represent some parts of your screen.
How many Fragment
's? As many as the Bottom Navigation's options.
Take the Youtube app as an example.
The whole screen is an Activity
, Youtube's MainActivity. As you can see the user pressed Subscriptions in the Bottom Navigation so the MainActivity called a SubscriptionsFragment
to draw the subscriptions part of the screen (all the layout except the ActionBar at the top and the Bottom Navigation at the bottom.
If a user selects Home at the Bottom Navigation then the MainActivity
will replace the part of the screen where the SubscriptionsFragment
draw its layout with the HomeFragment
's layout. And the same thing will happen with the other options of Youtube's Bottom Navigation bar.
So to clarify. There is only one activity here. The MainActivity
. Not four. This activity commands 4 fragments to draw the 4 main parts of its screen (the home part with HomeFragment
, the trending part with TrendingFragment
, the subscription part with SubscriptionFragment
and finally the library part with LibraryFragment
.
From your last comment:
I have to put the two pieces of code that I posted above into one big
class?
No you don't have to. It is not necessary to create one file, such as MainActivity.java (where your MainActivity
is defined), and then define the Fragment
's classes inside the same file.
You want to display 4 screens using a bottom navigation, right? Create a file for your activity and four separate files, one per Fragment
.
Example:
Your first file, SelectFriends.java, where SelectFriends fragment is defined, as you've posted above...
public class SelectFriends extends Fragment {
public SelectFriends () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_SelectFriends , container, false);
}
}
Three more separate files like this one, one per fragment.
And lastly your activity, again, as you've posted above.
public class MainActivity extends AppCompatActivity{
@Override
public void onCreate(Bundle savedInstanceState){
// ... some code here
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_item1:
..
// Here the activity figured out that the first item of the bottom navigation
// was clicked, so it calls the support fragment manager to display a fragment
// inside the container view
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
.replace(R.id.container, SelectFriends).commit();
return true;
case R.id.navigation_item2:
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
.replace(R.id.container, StartFood).commit();
return true;
}
return false;
}
}
Edit: Read the Ultimate Guide to Bottom Navigation on Android
thank you for your vast reply. I understand everything you told me, it´s like I assumed before. I also read the tutorial you linked and many more. But where exactly do I put all my Activities that I already created before starting with all that BottomNavigation stuff? I already had like at least 4 activities each one longer than 400 lines (I even used model layer for best practice). So where exactly do I place all that code which includes lines like: public class RestaurantListActivity extends AppCompatActivity { OR FloatingActionButton fab = findViewById(R.id.floatingActionButton);?
– Tony
Nov 12 at 2:34
First of all instead of thank me it would be better if you would upvote the answer if you found it useful or mark it as correct if that's the case.
– Skemelio
Nov 12 at 8:35
Now to your question: You already 4 activities and you want to convert them into 4 fragments. So you need to move the activities code into the fragments. As the docs say If you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.
– Skemelio
Nov 12 at 8:43
I created: Override public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { and put most of my code in it. Your answer was the most helpful to find the solution so I accepted it.
– Tony
Nov 18 at 0:01
add a comment |
Let's start from your question:
How to correctly put my activities in Tutorial´s Bottom Navigation?
You will not put your activities in the Bottom Navigation.
You will have one Activity
which will draw the layout of your screen. Secondly you will have a number of Fragment
's which will represent some parts of your screen.
How many Fragment
's? As many as the Bottom Navigation's options.
Take the Youtube app as an example.
The whole screen is an Activity
, Youtube's MainActivity. As you can see the user pressed Subscriptions in the Bottom Navigation so the MainActivity called a SubscriptionsFragment
to draw the subscriptions part of the screen (all the layout except the ActionBar at the top and the Bottom Navigation at the bottom.
If a user selects Home at the Bottom Navigation then the MainActivity
will replace the part of the screen where the SubscriptionsFragment
draw its layout with the HomeFragment
's layout. And the same thing will happen with the other options of Youtube's Bottom Navigation bar.
So to clarify. There is only one activity here. The MainActivity
. Not four. This activity commands 4 fragments to draw the 4 main parts of its screen (the home part with HomeFragment
, the trending part with TrendingFragment
, the subscription part with SubscriptionFragment
and finally the library part with LibraryFragment
.
From your last comment:
I have to put the two pieces of code that I posted above into one big
class?
No you don't have to. It is not necessary to create one file, such as MainActivity.java (where your MainActivity
is defined), and then define the Fragment
's classes inside the same file.
You want to display 4 screens using a bottom navigation, right? Create a file for your activity and four separate files, one per Fragment
.
Example:
Your first file, SelectFriends.java, where SelectFriends fragment is defined, as you've posted above...
public class SelectFriends extends Fragment {
public SelectFriends () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_SelectFriends , container, false);
}
}
Three more separate files like this one, one per fragment.
And lastly your activity, again, as you've posted above.
public class MainActivity extends AppCompatActivity{
@Override
public void onCreate(Bundle savedInstanceState){
// ... some code here
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_item1:
..
// Here the activity figured out that the first item of the bottom navigation
// was clicked, so it calls the support fragment manager to display a fragment
// inside the container view
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
.replace(R.id.container, SelectFriends).commit();
return true;
case R.id.navigation_item2:
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
.replace(R.id.container, StartFood).commit();
return true;
}
return false;
}
}
Edit: Read the Ultimate Guide to Bottom Navigation on Android
Let's start from your question:
How to correctly put my activities in Tutorial´s Bottom Navigation?
You will not put your activities in the Bottom Navigation.
You will have one Activity
which will draw the layout of your screen. Secondly you will have a number of Fragment
's which will represent some parts of your screen.
How many Fragment
's? As many as the Bottom Navigation's options.
Take the Youtube app as an example.
The whole screen is an Activity
, Youtube's MainActivity. As you can see the user pressed Subscriptions in the Bottom Navigation so the MainActivity called a SubscriptionsFragment
to draw the subscriptions part of the screen (all the layout except the ActionBar at the top and the Bottom Navigation at the bottom.
If a user selects Home at the Bottom Navigation then the MainActivity
will replace the part of the screen where the SubscriptionsFragment
draw its layout with the HomeFragment
's layout. And the same thing will happen with the other options of Youtube's Bottom Navigation bar.
So to clarify. There is only one activity here. The MainActivity
. Not four. This activity commands 4 fragments to draw the 4 main parts of its screen (the home part with HomeFragment
, the trending part with TrendingFragment
, the subscription part with SubscriptionFragment
and finally the library part with LibraryFragment
.
From your last comment:
I have to put the two pieces of code that I posted above into one big
class?
No you don't have to. It is not necessary to create one file, such as MainActivity.java (where your MainActivity
is defined), and then define the Fragment
's classes inside the same file.
You want to display 4 screens using a bottom navigation, right? Create a file for your activity and four separate files, one per Fragment
.
Example:
Your first file, SelectFriends.java, where SelectFriends fragment is defined, as you've posted above...
public class SelectFriends extends Fragment {
public SelectFriends () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_SelectFriends , container, false);
}
}
Three more separate files like this one, one per fragment.
And lastly your activity, again, as you've posted above.
public class MainActivity extends AppCompatActivity{
@Override
public void onCreate(Bundle savedInstanceState){
// ... some code here
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_item1:
..
// Here the activity figured out that the first item of the bottom navigation
// was clicked, so it calls the support fragment manager to display a fragment
// inside the container view
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
.replace(R.id.container, SelectFriends).commit();
return true;
case R.id.navigation_item2:
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
.replace(R.id.container, StartFood).commit();
return true;
}
return false;
}
}
Edit: Read the Ultimate Guide to Bottom Navigation on Android
edited Nov 11 at 21:31
answered Nov 11 at 20:54
Skemelio
749214
749214
thank you for your vast reply. I understand everything you told me, it´s like I assumed before. I also read the tutorial you linked and many more. But where exactly do I put all my Activities that I already created before starting with all that BottomNavigation stuff? I already had like at least 4 activities each one longer than 400 lines (I even used model layer for best practice). So where exactly do I place all that code which includes lines like: public class RestaurantListActivity extends AppCompatActivity { OR FloatingActionButton fab = findViewById(R.id.floatingActionButton);?
– Tony
Nov 12 at 2:34
First of all instead of thank me it would be better if you would upvote the answer if you found it useful or mark it as correct if that's the case.
– Skemelio
Nov 12 at 8:35
Now to your question: You already 4 activities and you want to convert them into 4 fragments. So you need to move the activities code into the fragments. As the docs say If you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.
– Skemelio
Nov 12 at 8:43
I created: Override public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { and put most of my code in it. Your answer was the most helpful to find the solution so I accepted it.
– Tony
Nov 18 at 0:01
add a comment |
thank you for your vast reply. I understand everything you told me, it´s like I assumed before. I also read the tutorial you linked and many more. But where exactly do I put all my Activities that I already created before starting with all that BottomNavigation stuff? I already had like at least 4 activities each one longer than 400 lines (I even used model layer for best practice). So where exactly do I place all that code which includes lines like: public class RestaurantListActivity extends AppCompatActivity { OR FloatingActionButton fab = findViewById(R.id.floatingActionButton);?
– Tony
Nov 12 at 2:34
First of all instead of thank me it would be better if you would upvote the answer if you found it useful or mark it as correct if that's the case.
– Skemelio
Nov 12 at 8:35
Now to your question: You already 4 activities and you want to convert them into 4 fragments. So you need to move the activities code into the fragments. As the docs say If you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.
– Skemelio
Nov 12 at 8:43
I created: Override public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { and put most of my code in it. Your answer was the most helpful to find the solution so I accepted it.
– Tony
Nov 18 at 0:01
thank you for your vast reply. I understand everything you told me, it´s like I assumed before. I also read the tutorial you linked and many more. But where exactly do I put all my Activities that I already created before starting with all that BottomNavigation stuff? I already had like at least 4 activities each one longer than 400 lines (I even used model layer for best practice). So where exactly do I place all that code which includes lines like: public class RestaurantListActivity extends AppCompatActivity { OR FloatingActionButton fab = findViewById(R.id.floatingActionButton);?
– Tony
Nov 12 at 2:34
thank you for your vast reply. I understand everything you told me, it´s like I assumed before. I also read the tutorial you linked and many more. But where exactly do I put all my Activities that I already created before starting with all that BottomNavigation stuff? I already had like at least 4 activities each one longer than 400 lines (I even used model layer for best practice). So where exactly do I place all that code which includes lines like: public class RestaurantListActivity extends AppCompatActivity { OR FloatingActionButton fab = findViewById(R.id.floatingActionButton);?
– Tony
Nov 12 at 2:34
First of all instead of thank me it would be better if you would upvote the answer if you found it useful or mark it as correct if that's the case.
– Skemelio
Nov 12 at 8:35
First of all instead of thank me it would be better if you would upvote the answer if you found it useful or mark it as correct if that's the case.
– Skemelio
Nov 12 at 8:35
Now to your question: You already 4 activities and you want to convert them into 4 fragments. So you need to move the activities code into the fragments. As the docs say If you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.
– Skemelio
Nov 12 at 8:43
Now to your question: You already 4 activities and you want to convert them into 4 fragments. So you need to move the activities code into the fragments. As the docs say If you're converting an existing Android application to use fragments, you might simply move code from your activity's callback methods into the respective callback methods of your fragment.
– Skemelio
Nov 12 at 8:43
I created: Override public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { and put most of my code in it. Your answer was the most helpful to find the solution so I accepted it.
– Tony
Nov 18 at 0:01
I created: Override public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { and put most of my code in it. Your answer was the most helpful to find the solution so I accepted it.
– Tony
Nov 18 at 0:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243271%2fhow-to-correctly-put-my-activities-in-tutorial%25c2%25b4s-bottom-navigation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I've noticed that this is at least the third time that you've posted this same question (stackoverflow.com/q/53219136, stackoverflow.com/q/53234243). Firstly, please don't repeat questions. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working, will bump it to the top of the active queue. Secondly, it seems that you might have a fundamental misunderstanding of the nature of Activities and Fragments.
– Mike M.
Nov 10 at 21:09
Fragments do not have a one-to-one correspondence to Activities, and you do not need a separate
Activity
class for eachFragment
class. A singleActivity
can host and manipulate multipleFragment
s, which seems to be what you're looking to do. When you say that you have "4 working activities with 4 xml layouts", it seems that you actually want fourFragment
s hosted within a singleActivity
. If that's the case, you simply need to transact the pertinentFragment
s within that singleActivity
.– Mike M.
Nov 10 at 21:09
@Mike M. Thank you for replying, it seems you understood my issue. My question is how do I transact the pertinent Fragments within that single Activity? By putting all 4 Activities somewhere into public class SelectFriends extends Fragment? Isn´t it bad practice because of battery drain: Everytime the App is opened that big activity code must be loaded even when the user intented to use only a litte part (1 activity) of the App...
– Tony
Nov 10 at 22:10
As @Mike M. stated above you need one Activity which will hold your 4 other fragmens. After your activity is created it will load the first fragment. Then if you press a button in the navigation the activity will replace the current fragment with another one and so on...
– Skemelio
Nov 11 at 2:13
@Skemelio If I understand you right, I have to put the two pieces of code that I posted above into one big class, and the corresponding Activity where "// Required empty public constructor" is falsely written. Then I do the same with the other 3 Activities: I write a new class "StartFood" into that one big class. Can you give me a short example please, if I am wrong? Thank you very much!
– Tony
Nov 11 at 19:06