Actionbar activity and fragment manager - android

I'm using an ActioBarActivity in order to use the new toolbar from appcompact v21.
But, when using this type of activity, when I replace a fragment with de parameter addToBackStack(), it doesn't work.
When the user presses the back button, the role activity is destroyed.
This is my onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout), mToolbar,getSupportActionBar());
if (savedInstanceState == null){
getFragmentManager().beginTransaction()
.replace(R.id.container, ListaFragment.newInstance(""))
.commit();
}
}
And this is the click that replaces the fragment
#Override
public void helpClick(int stringResource) {
getFragmentManager().beginTransaction()
.replace(R.id.container, HelpFragment.newInstance(stringResource))
.addToBackStack(null)
.commit();
}
I don't know if there is an compability erro when using the import android.support.v7.app.ActionBarActivity with the import android.app.Fragment;

Had the same problem. You should use classes from the support library: android.support.v4.app.FragmentManager and android.support.v4.app.Fragment instead of default ones.
So you have to replace getFragmentManager() with the getSupportFragmentManager() and import android.app.Fragment; with import android.support.v4.app.Fragment;.
Also your fragments should extend android.support.v4.app.Fragment class.

Related

how to set backspace button in place ActionbarDrawerToggle to navigate to previous fragments?

I am calling fragments from Main_activity.java and also calling fragments from other fragments but i want to show backspace(<-) arrow button when any fragment is loaded(either from Main_activity or any other fragments) instead of ActionBarDrawerToggle. how can I achieve this?
Main Activity code
public class MainActivity extends AppCompatActivity {
NavigationView nav;
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayout;
Toolbar toolbar;
ImageView homeInToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nav=findViewById(R.id.navigationview);
drawerLayout=findViewById(R.id.drawer);
// homeInToolbar=findViewById(R.id.homeInToolbar);
toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
FragmentManager manager=getSupportFragmentManager();
nav.setCheckedItem(R.id.menu_home);
FragmentTransaction transaction=manager.beginTransaction();
manager.beginTransaction().replace(R.id.framelayout,new HomeFragment()).commit();}
Main_Activity where the toogle is shown and after click on students, the image should look like
Add these 2 lines after setSupportActionBar(toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
This works for me.
In main_activity from where I calling the fragment, I have disabled toggle as shown in below code.
public void onClick(View v) {
toggle.setHomeAsUpIndicator(R.drawable.ic_back);
toggle.setDrawerIndicatorEnabled(false);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,new FeeFragment(toggle)).addToBackStack(null).commit();
}
});
and where ever necessary, I can enable toggle by below code.
toggle.setDrawerIndicatorEnabled(true);

Android Hamburger Menu and Back Arrow

I want to make on my MainActivity a property navigation for my users with hamburger and back arrow.
When I have only one fragment set hamburger, but if i have more fragments added on my MainActivity, set the back arrow.
How do I implement that?
Here is my implementation...
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
private DrawerLayout drawer;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar(toolbar);
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
//TODO: Insert back arrow button if have more than one fragment on backstack
/*getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
int stackHeight = getSupportFragmentManager().getBackStackEntryCount();
if (stackHeight > 0) {
if (getSupportActionBar() != null) {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toggle.setDrawerIndicatorEnabled(false);
}
} else {
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(true);
}
}
}
});*/
ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), new ListVeiculoFragment(), R.id.container_main);
}
}
Work by creating Interface class:
public interface HideShowIconInterface{
void showHamburgerIcon();
void showBackIcon();
}
Implement Interface in your Activity:
public class YourActivity extends AppCompatActivity implements HideShowIconInterface{
#Override
public void showHamburgerIcon() {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
mActionBarDrawerToggle.setDrawerIndicatorEnabled(true);
}
#Override
public void showBackIcon() {
mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
In your Fragment, call whatever you want by:
((HideShowIconInterface) getActivity()).showHamburgerIcon();
or
((HideShowIconInterface) getActivity()).showBackIcon();
You have to catch the moment when the numbers of the fragments change and use this code to hide (false) / show (true) the DrawerToggle "Hamburger"
mDrawerToggle.setDrawerIndicatorEnabled(false);
EDIT:
In your Activity (somewhere) you have something like this (where you change current Fragment):
private void selectItem(int position) {
Fragment fragment = new MyFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
}
I would change last line by this:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack(null)
.commit();
and then check:
if(fragmentManager.getBackStackEntryCount() > 1) mDrawerToggle.setDrawerIndicatorEnabled(false);
setSupportActionBar(toolbar);
toolbar.setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
put this code in your activity where you use humbarger style navigation it work for me.
Actually none of above solutions works fully.
There are couple of issues that have to be considered -
a) if using ActionBarDrawerToggle then getSupportActionBar().setDisplayHomeAsUpEnabled is now working as expected - it actually replaces icon for toggle, but clicks are still handled by toggle plus hamburger icon is hidden when toggle is enabled again
b) As fragments transaction (add, restore form backstack) are done asynchronously, check for actual backstack size has to be done after fragment transaction is done - e.g. from fragments onCreateView
Here is code that works for me (in Kotlin):
override fun onCreate(savedInstanceState: Bundle?) {
// ....
drawerToggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(drawerToggle)
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_arrow_back_white)
drawerToggle.syncState()
drawerToggle.setToolbarNavigationClickListener {
// whatever action is needed on homeAsUp click
onBackPressed()
}
//And this method should be called from fragment's onCreateView
fun showUpNavigation() {
drawerToggle.isDrawerIndicatorEnabled=supportFragmentManager.getBackStackEntryCount() <= 1
}
You just add
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
For more details see Android - Switch ActionBar Back Button to Navigation Button.

Can't stop actionBar home icon from acting as a button

I need to stop the app icon in the upper left corner on the actionBar from acting as a button. I tried using actionBar.setHomeEnabled(false), but it isn't working and i'm not sure why. Any help is really appreciated. This is my onCreate method:
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound_board);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if(fragment == null){
fragment = new SoundBoardFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
}
}
You will also need to call setDisplayHomeAsUpEnabled(false)

How to set a default fragment in Android Studio Navigation Drawer?

I'm looking for a way to se a default fragment in my navigation drawer which is not the item # 0. I've seen some solutions based on checking if savedInstanceState is null or not but I feel like I don't have access to this in my onNavigationDrawerItemSelected method. Here is my class:
public class Main extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the Main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new HomeFragment();
break;
case 2:
fragment = new HomeFragment();
break;
default:
fragment = new DiscoverFragment();
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
}
The answer is rather simple. Since the DrawerLayout leaves you to manage fragments on your own, you can just use the basic FragmentManager APIs that would be used in a normal FragmentActivity.
I use the following method to make it a little simpler, which will replace the current fragments even if there are none currently added to the container. Just call this method with the fragment you want to be default in your onCreate() method.
public void setFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
Note that it is technically more correct to use .add(fragment) for the first call rather than .replace since you aren't replacing anything, but it really doesn't matter because internally, it basically just removes any existing fragments and adds the new one using .add() for you
http://developer.android.com/reference/android/app/FragmentTransaction.html#replace(int,android.app.Fragment,java.lang.String)

Sliding menu using jfeinstein10

I am using jfeinstein10 library and sample project. I want to show some menu option in left sliding menu and also some other option menu in the right sliding menu. But i am not able to do that . Here is the code :
package com.slidingmenu.example;
public class LeftAndRightActivity extends BaseActivity {
public LeftAndRightActivity() {
super(R.string.left_and_right);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
setContentView(R.layout.content_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new SampleFragment())
.commit();
getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame_two, new SampleFragment())
.commit();
}
}
Here it is what i have set in the onCreate :
getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
setContentView(R.layout.content_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new SampleListFragment())
.commit();
getSlidingMenu().setMenu(R.layout.menu_frame_two);
getSlidingMenu().setShadowDrawable(R.drawable.shadowright);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame_two, new SampleListFragment2())
.commit();
getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame_two, new SampleListFragment2())
.commit();
here whenever i use this my program get crashed.And here is my logcat :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.slidingmenu.example/com.slidingmenu.example.LeftAndRightActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c for fragment Fragment{41260b30 #0 id=0x7f05003c}

Categories

Resources