I am using the navigation drawer in Android Studio. When I select an item in navigation drawer I'm using the following code:
public void onNavigationDrawerItemSelected(int position) {
switch(position)
{
case 0:
Intent intent1 = new Intent(MainActivity.this,HomeActivity.class);
startActivity(intent1);
break;
case 1:
Intent intent2 = new Intent(MainActivity.this,DayActivity_1.class);
startActivity(intent2);
break;
}
}
When I call my activities from the navigation drawer item selected the action bar disappears and activities open on full screen. How can I manage that the navigation drawer doesn't disappear?
If you want to persist navigation drawer you should change content fragment, instead of showing Activity.
In your case it would be, change
Intent intent1 = new Intent(MainActivity.this,HomeActivity.class); startActivity(intent1);
with:
FragmentManager fragmentManager = ...
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.your_fragment_container_id, new HomeFragment())
transaction.commit();
dont use DayActivity_1.class activity use fragment instead and when you click on navigation item jst create fragment and replace it to the drawerlayout
Extend FragmentActivity in you main navigation activity class like below.
public class NavigationdrawerActivity extends FragmentActivity
Now use the below code to solve your problem
public void setContent(Fragment fragment) {
// Fragment fragment = new content_home();
FragmentTransaction fragmentManager = getFragmentManager().beginTransaction();
fragmentManager.setCustomAnimations(R.animator.enter_from_left, R.animator.exit_to_left);
// fragmentManager.beginTransaction()
fragmentManager.replace(R.id.mainContent, fragment).commit();
}
public void onNavigationDrawerItemSelected(int position) {
switch(position)
{
case 0:
break;
case 1:
Intent intent2 = new Intent(MainActivity.this,DayActivity_1.class);
startActivity(intent2);
break;
}
}
public void onNavigationDrawerItemSelected(int position) {
switch(position)
{
case 0:
Fragment homeActivityFragment = new HomeActivityFragment();
//if you want to pass data to fragment
//Bundle bundle = new Bundle();
//bundle.putString("id", "" + item.get("id"));
//homeActivityFragment.setArguments(bundle);
setContent(homeActivityFragment)
break;
case 1:
Fragment dayActivity_1Fragment = new DayActivity_1Fragment();
setContent(dayActivity_1Fragment)
break;
}
}
Related
I have a sliding menu with some Fragments, but i want to change it with Activities, is it possible?
private void replaceFragment(int pos) {
Fragment fragment = null;
switch (pos) {
case 0:
fragment = new X();
break;
case 1:
fragment = new Y();
break;
case 2:
fragment = new Z();
break;
default:
fragment = new X();
break;
}
if (null != fragment) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_content, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
I don't know how to change the last part of the code. --> FragmentManager and --> FragmentTransaction. Thanks!!
Yes you can use Activity in place of fragment, But
you have following challenges.
You need to implement sliding drawer on every activity, if you uses fragment then HomeActivity sliding drawer will be shown on all fragment
update your code like this if you want to implement activity
private void replaceFragment(int pos) {
Intent intent = null;
Context context = this;
switch (pos) {
case 0:
intent = new Intent(context, SecondActivity.class);
break;
case 1:
intent = new Intent(context, ThirdActivity.class);
break;
case 2:
intent = new Intent(context, FourthActivity.class);
break;
default:
intent = new Intent(context, DefaultActivity.class);
break;
}
if (intent != null) {
startActivity(intent);
}
}
i want call simple Android Activity On click of Slider Menu Item, on place of fragment.
i tried to called activity using fragment but it shows nothing
please help me.
can anyone suggest alternate way rather than this ?
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
Log.e("", "In Fragment "+position);
fragment = new About_us_Activity();
//startActivity(new Intent(this,Home_Activity.class));
break;
case 1:
//fragment = new About_us_frag();
Log.e("", "In Fragment "+position);
break;
case 2:
Log.e("", "In Fragment "+position);
fragment = new About_us_frag1();
break;
default:
break;
}
if (fragment != null) {
Log.e("", "In Fragment");
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
First of all apply onItemClickListener to your drawer list.
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mDrawerLayout.closeDrawers();
displayView(position);
}
});
In your DisplayView method:
private void displayView(int position) {
switch (position) {
case 1:
Intent intent = new Intent(getApplicationContext(), MyActivity1.class);
startActivity(intent);
break;
case 2:
Intent intent1 = new Intent(getApplicationContext(),MyActivity2.class);
startActivity(intent1);
break;
default:break;
}
}
MyActivity1 and MyActivity2 are activities. you can try this method , it worked for me.
So I have my navigation drawer with 5 different options. They all open a new fragment that I have created. The first one is Home, and I am trying to find a way to bring it back to the first screen that shows up under the navigation drawer. It has the id of "container", in the main_activity.xml. I do not want to use and intent to call the entire class again to load up. Also I do not want to be able to use the back button from another intent. I am confused on how to make this happen.
#Override
public void onNavigationDrawerItemSelected(int position) {
FragmentHowItWorks fragmentHow;
FragmentSettings fragmentSettings;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
switch(position){
case 0:
// should I call the layout?
// this is the "Home" option
break;
case 1:
fragmentHow = new FragmentHowItWorks();
transaction.replace(R.id.container, fragmentHow);
transaction.addToBackStack(null);
transaction.commit();
break;
case 2:
fragmentSettings = new FragmentSettings();
transaction.replace(R.id.container, fragmentSettings);
transaction.addToBackStack(null);
transaction.commit();
break
case 3:
fragment = new FragmentHowItWorks();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case 4:
fragment = new FragmentHowItWorks();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
break;
}
}
Use methods add,hide and show,
Step1 Prepare all your fragments
Fragment fragment1 = new FragmentOne();
Fragment fragment2 = new FragmentTwo();
Fragment fragment3 = new FragmentThree();
Fragment mCurrentFragment = null;
Step2 Show/hide your fragments
#Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
switch (position) {
case 1:
fragment = fragment1;
break;
case 2:
fragment = fragment2;
break;
case 3:
fragment = fragment3;
break;
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if(mCurrentFragment == null) {
ft.add(R.id.container, fragment).commit();
mCurrentFragment = fragment;
} else if(fragment.isAdded()) {
ft.hide(mCurrentFragment).show(fragment).commit();
} else {
ft.hide(mCurrentFragment).add(R.id.container, fragment).commit();
}
mCurrentFragment = fragment;
}
You can do this ,
You can get the name of current fragment which is in the container . This will return name including the package + fragment name
String name = getFragmentManager().findFragmentById(container id ).getClass().getName();
When you click on the home index of drawer, check weather the current name id equal to the home.
If it is home, you don't need to perform any action.
I know that this was asked and answered long time ago but, I would like to show my approach on this problem, maybe it will help anyone else:
I added a Fragment Name over each Fragment that I use like:
public class MainFragment extends BaseFragment {
public static final String FRAGMENT_TAG = "main";
// ... all your fragment
}
And on the Drawer Layout:
public void methodThatSetsTheUi() {
mDrawerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
getFragmentManager().findFragmentByTag("main");
String tag = getFragmentTag(position);
replaceFragment(getOrCreateFragment(tag), tag);
mDrawerLayout.closeDrawer(mDrawerView);
}
});
}
#NonNull
private String getFragmentTag(int position) {
String tag;
switch (position) {
case MAIN_FRAGMENT_DRAWER_POSITION:
tag = MainFragment.FRAGMENT_TAG;
break;
case FAVORITE_FRAGMENT_DRAWER_POSITION:
tag = FavoriteFragment.FRAGMENT_TAG;
break;
default:
throw new AssertionError("That selection is wrong");
}
return tag;
}
private BaseFragment getOrCreateFragment(String fragmentName) {
BaseFragment fragment = (BaseFragment) getFragmentManager().findFragmentByTag(fragmentName);
if(fragment == null) {
fragment = FragmentFactory.createFragment(fragmentName);
}
return fragment;
}
and well, the FragmentFactory is just a simple Factory:
public final class FragmentFactory {
public static BaseFragment createFragment(String fragmentName) {
switch(fragmentName) {
case MainFragment.FRAGMENT_TAG:
return MainFragment.newInstance();
case FavoriteFragment.FRAGMENT_TAG:
return FavoriteFragment.newInstance();
// ... all fragments here.
default:
return null;
}
}
Hope to help someone.
What i have is left navigation menu that i have made using navigation drawer and it works just fine with fragments , and i have five buttons each one open a fragment but now i want each case to open an activity instead of fragment , and i have tried to do it using intent but it didn't work !!
here is my code :
private void displayView(int position) {
// update the main content by replacing fragments
android.app.Fragment fragment = null;
switch (position) {
case 0:
//fragment = new HomeFragment();
// Intent i=new Intent(MainActivity.this,MainActivity.class);
// startActivity(i);
break;
case 1:
//fragment = new FindPeopleFragment();
break;
case 2:
//fragment = new PhotosFragment();
break;
case 3:
//fragment = new CommunityFragment();
break;
case 4:
//fragment = new PagesFragment();
break;
case 5:
//fragment = new WhatsHotFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
So what shall i change in this code so it can open an activity instead of fragment??? can anyone help me?
You need to change your function to load a class Fragment in some case and in other cases a class Activity
case 0:
activity = new MyActivity();
fragment = null;
break;
case 1:
fragment = new MyFragment();
activity = null;
break;
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, (android.app.Fragment) fragment).commit();
setTitle(menutitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}else {
if(activity != null) {
Intent i1 = new Intent(MainActivity.this, activity.getClass());
i1.putExtra(EXTRA_MESSAGE, position);
startActivity(i1);
setTitle(menutitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
Could you please post the Error from Logcat:
What i would check first is:
Are the Activities declared in the Manifest.xml as Activies?
Secondly check if you calling Activities with an Intent, and not Fragments
This is how you start Activities correctly.
Intent intent = new Intent(mActivity, class1); // class1 The class to open
startActivity(intent);
This is one of the method() of Navigation Drawer activity...
Code Snippet:
/**
* Diplaying fragment view for selected nav drawer list item
**/
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ViewFragment();
break;
case 1:
//here i should call the FragmentActivity
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// Log.e("MainActivity", "Error in creating fragment");
}
}
create intent with fragment activity and use startActivity() to launch the fragment Activity.
switch (position) {
case 0:
fragment = new ViewFragment();
break;
case 1:
//here i should call the FragmentActivity
Intent intent = new Intent(Context, FragmentActivity.class);
startActivity(intent);
default:
break;
}
But make sure that your FragmentActivity. In FragmentActivity you have to use fragment to display the content.