I've downloaded the sample project from the Android official site http://developer.android.com/training/implementing-navigation/nav-drawer.html. I am trying to understand how the Navigation Drawer works. So, I have one doubt, they call a Fragment for each item from the left menu. In my project, I have a big activity which I am trying to call by this Fragment:
public class HomeFragment extends Fragment {
public HomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent intent = new Intent();
intent.setClass(getActivity(), ListMatch.class);
startActivity(intent);
View rootView = inflater.inflate(R.layout.list_match, container, false);
return rootView;
}
}
But, if I do that, it calls perfectly my Activity, but the menu disappear. How can I call this activity and keep my menu? Thanks a lot.
Like Raghunandan said, the drawer applies wihin a single activity. It's common to launch a new activity from the "main" drawer, but usually that kind of activity would have the up action set in the action bar to go back to the main activity.
Related
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
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
I have my Activity that displays 3 tabs. I want to call an activity (extends AppCompatActivity) that contains textviews, buttons, editboxes for displaying data from SQLite. When I click the second tab, the activity is displayed but it occupies the whole screen and is not being displayed inside the second tab.
I am using a pager class for my tabs.
This is the fragment tab that should display my activity inside it:
public class settings_tab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
return inflater.inflate(R.layout.activity_settings_tab, container, false);
}
}
When I call the activity, it is displayed but it occupies the whole screen and is not being displayed inside the second tab
Activities-in-tabs has been deprecated as a technique for six years.
Please use something else for your tabs, such as fragments.
I am using navigation drawer in my application, with fragments. There are icons and text in each row(one of them is products, on clicking these a fragment is open with a list view.After clicking on the list view row i have an activity in this i want header name same as i have selected(products).but bydefault it is showing name off the app which is sales.please help.
pass product name in Intent in your list's onItemClickListner
Intent intent = new Intent(getActivity(),YOURACTIVITY.class);
intent.putExtra("product_name",YOUR PRODUCT_NAME);
getActivity().startActivity(intent);
and in your target activity (your product detail activity) put below code in onCreate() method.
getSupportActionBar().setTitle("YOUR DESIRED TITLE");
If using Frament , you can have a method in parent hosting Activity like
this
public void setActionBarTitle(String title) {
getSupportActionBar().setTitle(title);
}
and invoke this method from your individual fragment like this and set the title
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//usual statements
((HomeActivity)getActivity()).setActionBarTitle("Child fragment name");
}
You can set the actionbar header with the following. You can try this.
ActionBar actionBar = getActionBar();
actionBar.setTitle("My Title");
I am using Fragments and Activity extended by AppCompatActivity in an application.
Working:
I have two fragments "Dashboard" and "Order". I am replacing fragment after clicking "Order Button" from Dashboard Fragment, and coming back to "Dashboard Fragment" after pressing back button.
Problem
I have called an API on onCreateView() of Dashboard Fragment. When I press back from Order Fragment then I come to "Dashboard Fragment" the it recall the API. I don't want to recall the API if I come to the fragment by back press.
Thanks in advance.
Code to replace dashboard fragment with order fragment
// Click event of Order
#OnClick(R.id.ll_order)
void openOrder() {
if (isOrderNotClicked) {
OrderFragment fragment = new OrderFragment();
this.getFragmentManager().beginTransaction().replace(R.id.flContent, fragment, "Order").addToBackStack(null).commit();
isOrderNotClicked = !isOrderNotClicked;
}
}
I did had same problem so i solved by `View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view == null) {
//inflate layout and codes
}
return view;
}` but i dont know it is the best solution
I have decided my layout on the basis of JSON response and make a layout at run time without any xml . I just take a Activity and call a api that give me json array on the basis of which i make my layout. I have no problem with that but now i want to attach a navigation drawer with this activity . I have used navigation drawer in my previous Activity by making them fragment but in this case i don't know how to convert my activity to fragment because in navigation drawer we need fragment not activity.
i just do not know how to convert this Dynamic ui to fragment
View rootView = inflater.inflate(R.layout.`dashboard`,container, false);
what should i write in place of dashboard because i have no xml for that particular activity ..
I did it this way:
public class BaseFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ScrollView scrollView = new ScrollView(getActivity());
View view = //view from JSON
scrollView.addView(view);
return scrollView;
}
}
in navigation drawer handler
getFragmentManager().beginTransaction().replace(R.id.content_frame, new BaseFragment()).commit();