I am developing android application with drawer layout in fragments. actually i am trying to display drawer layout in oncreate in fragments but it is not coming
i have oncreate and oncreateview override method in fragments and whenever application open that time only my drawer layout should come.
this is my oncreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.test2, container, false);
mDrawerLayout = (DrawerLayout)rootView.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) rootView.findViewById(R.id.list_slidermenu);
mDrawerLayout.openDrawer(Gravity.LEFT);
navDrawerItems = new ArrayList<String>();
navDrawerItems.add("first");
navDrawerItems.add("second");
navDrawerItems.add("third");
adapter = new NavDrawerListAdapter(getActivity(),navDrawerItems);
mDrawerList.setAdapter(adapter);
return rootView;
}
My Requirements:-
My loadingswipe() function should execute whenever application open that is the reason i am giving loadingswipe() inside oncreate. if i give this function in oncreateview, it is executing while clicking back button also but my requirement is it should not execute while clicking back button. could you please help me to resolve this issues.
Update:-
Now i have given my drawer layout in my oncreateview Now my drawer layout is showing but the problem is inside test2 i have one button if click that enter into that page but whenever i click back button that time drawer layout also showing but for me that button only should show
Related
I have a Navigation Drawer with 6 Fragments. One of them should contain a ListView. I got it to work and I can switch through all the Fragments via the Navigation Drawer. When I press on the one containing the ListView I can see it, just as it should be. However, when I go to the same Fragment (containing the ListView) while I'm in it already, the ListView isn't displayed anymore. This is probably due to the fact, that I set the ListView on the onActivityCreated() method in the Fragment. I'm curious now though, what happens when I press on the same Fragment again. Does the activity stay loaded and the Fragment needs to be inflated again and through this the OnActivityCreated() method isn't called again? And if so, what do I have to do, that the ListView gets displayed again?
My Fragment.class currently looks like this:
public class FahrplanFragment extends Fragment {
private ListView listView;
private Resources res;
private String[] plans;
private ArrayAdapter<String> adapter;
public FahrplanFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_fahrplan, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = (ListView) getActivity().findViewById(R.id.listview_fahrplan);
res = getResources();
plans = res.getStringArray(R.array.plans);
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
android.R.id.text1, plans);
listView.setAdapter(adapter);
}
}
As in the documentation
Called when the fragment's activity has been created and this
fragment's view hierarchy instantiated. It can be used to do final
initialization once these pieces are in place, such as retrieving
views or restoring state. [..] This is called after
onCreateView(LayoutInflater, ViewGroup, Bundle) and before
onViewStateRestored(Bundle).
It is called every time. You can check by logging the event.
Can you post the code of the event handling when click on the navagition drawer button?
EDIT 1
Here I re-created you project to test it and it works. The fragment loads every time with the list view inside. Moreover, it displays the events when they happens and shows clearly that onActivityCreated is called every time the fragment is replaced.
The best suggestion I can give here is to log your code using Log.d() or System.out.println("...") in various steps of the code, or even better enter in debug mode by setting breakpoints and inspect the code while it's running to understand when it enter in some methods and when not.
I am currently working on an application with ActionBar tabs inside one of its fragments (main navigation is NavigationDrawer). The fragment's onCreate():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBar = activity.getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Resources r = getResources();
//(…) tabNames initialization
adapter = new CustomTabAdapter(getFragmentManager());
}
and onCreateView():
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(this);
for (String tabName : tabNames) {
actionBar.addTab(actionBar.newTab()
.setText(tabName)
.setTabListener(this));
}
return rootView;
}
There are 4 tabs, each of them with its own Fragment and layout. CustomTabAdapter returns new instance of proper fragment in its getItem(int). There are 4 tabs.
And now my problem is:
I start the application.
I choose this tab fragment from NavigationDrawer list. Everything is working just fine.
I choose another fragment from NavigationDrawer list which means that tab fragment is replaced with it.
I choose tab fragment again. And fragment related to tab that was selected before choosing another fragment from NavigationDrawer list, and one's adjacent to it are not recreated (blank screen under ActionBar tabs). I checked and onCreateView(…) methods of those fragment are not called. So after changing device orientation, or choosing tab not adjacent and this one again proper layout is shown.
How can I make it work as it should (showing proper layout on reentering tab fragment from NavigationDrawer list instead of blank space)? I run out of ideas.
Finally, I found a solution. My CustomTabAdapter was extension of FragmentPagerAdapter. I changed it to be extension of FragmentStatePageAdapter, and now fragments are recreated.
More details in this answer by #Louth.
It takes fragments from cache without recreation
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();
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.
I have a tabbed application using fragments. Following is a code snippet from one of the fragments in tabA
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
accountsView = inflater.inflate(R.layout.activity_accounts, viewGroup, false);
//obtain the labels- Top 50 Spending aacounts
spendingLabel = (TextView) accountsView.findViewById(R.id.textView1);
//get search box
accountSearch = (EditText) accountsView.findViewById(R.id.vAccountSearch);
return accountsView;
}
Here is the onStart()
#Override
public void onStart() {
super.onStart();
fillData();
}
fillData() fetches dat from the server. The application now fills a listView on tab click (by calling fillData() ). If I switch to other tabs and come to tabA, the page again calls fillData() and recreates the whole view.
How do I stop it reloading and keep the view unchanged on tab switching?
Since I am a newbie into Fragments, I find it hard to find a solution. Has anyone dealt this before?
According to the lifecycle of Android application try to override onCreate instead of onStart
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fillData();
}