Earlier, I was having an issue with onBackPressed and got it resolved with some help here.
After doing some research, I found out that due to having my starting fragment in the backstack, it's producing an extra 'back' count so when I attempt to exit my app, I get a blank screen:
Instead of the blank screen I should get a prompt for "Are you sure to exit" instead of the blank. From the blank screen, I do get my 'are you sure to exit' menu, but I'd prefer not seeing an extra blank screen.
I've also noticed that it seems to not update the fragment titles on the back pressed, but I'm assuming that's another issue all together.
I'm unsure how to separate my home fragment from the backStack count. If I'm to avoid it from being added to the stack, but leave it as a navigational item, I'm confused on how that would even work.
Code:
public class Home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private Fragment fragment = null;
private FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DatabaseHelper myDbHelper = new DatabaseHelper(Home.this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
// getHome();
displayView(R.id.nav_large_monsters);
}
private boolean viewIsAtHome;
#Override
public void onBackPressed() {
// Pressing back popped the back stack, nothing else to do
FragmentManager fragmentManager = getSupportFragmentManager();
if (!fragmentManager.isStateSaved() && fragmentManager.popBackStackImmediate()) {
return;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
if (!viewIsAtHome) {
displayView(R.id.nav_large_monsters);
} else {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Home.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
private void getHome(){
fragment = new HomeFragment();
if (fragment != null) {
FragmentManager fragmentMgmt = getSupportFragmentManager();
fragmentMgmt.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
displayView(item.getItemId());
return true;
}
public void displayView(int viewId) {
// Fragment fragment = null;
String title = getString(R.string.app_name);
switch (viewId) {
default:
fragment = new Large_Monsters();
title = "Large Monsters";
break;
case R.id.nav_large_monsters:
fragment = new Large_Monsters();
title = "Large Monsters";
viewIsAtHome = true;
break;
case R.id.nav_small_monsters:
fragment = new Small_Monsters();
title = "Small Monsters";
viewIsAtHome = false;
break;
case R.id.nav_weapons:
fragment = new Weapons();
title = "Weapons";
viewIsAtHome = false;
break;
case R.id.nav_armors:
fragment = new Armors_Low_High();
title = "Armor Sets";
viewIsAtHome = false;
break;
case R.id.nav_charms:
fragment = new Charms();
title = "Charms";
viewIsAtHome = false;
break;
case R.id.nav_items:
fragment = new Items();
title = "Items";
viewIsAtHome = false;
break;
case R.id.palico_armor:
fragment = new Palico_Armor();
title = "Palico Armor";
viewIsAtHome = false;
break;
case R.id.palico_gadgets:
fragment = new Palico_Gadgets();
title = "Palico Gadgets";
viewIsAtHome = false;
break;
case R.id.palico_weps:
fragment = new Palico_Weapons();
title = "Palico Weapons";
viewIsAtHome = false;
break;
case R.id.palico_helms:
fragment = new Palico_Helms();
title = "Palico Helmets";
viewIsAtHome = false;
break;
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment, fragment.getTag());
ft.addToBackStack(null);
ft.commit();
}
// set the toolbar title
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
}
You just don't have to call addToBackStack(null) on your Home page(ex: Large_Monsters fragment)
so just add in your onCreate method like this
Large_Monsters fragment = new Large_Monsters();
if (fragment != null) {
FragmentManager fragmentMgmt = getSupportFragmentManager();
fragmentMgmt.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
In your left side menu, you just need to check whether this fragment exists or not, if not then replace it otherwise remove other fragments.
I think I figured it out just after posting this...
The section here in the onBackPressed():
if (!viewIsAtHome) {
displayView(R.id.nav_large_monsters);
}
Was there to help set the check for what's the home page, but IF I use my getHome() method, and set that under OnCreate:
private void getHome(){
fragment = new Large_Monsters();
if (fragment != null) {
FragmentManager fragmentMgmt = getSupportFragmentManager();
fragmentMgmt.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
}
This let's me set my home page, and avoids the double check at onBackPressed. But mainly, this avoids setting a check at my onNavigationItemSelected area and let's android handle onBackPressed as usual.
Related
I would like to make a bottom navigation bar in my project. Every view has it's own fragment. The problem is that every time i click on the button to change the view for example from recents to favorites it creates new fragment with completely new states(e.g. scroll position, text changed whatever my fragment contains). I know that in official Android documentation there was written that bottom navigation bar should reset the task states, but i think it is too uncomfortable for users.
I would like to have kind of similar functionality like instagram that you change from feed to explore and back to feed the scroll position the image caches everything remains saved. I tried almost every way to solve this problem the only thing that worked is by setting visibility GONE and setting visibility VISIBLE according to situation but i understand that it is not RIGHT way there should be better way of doing this and i am not talking about manually saving needed instances. I followed almost every tutorial about bottom nav fragments but the interesting thing is that no one is interested to make it use without calling new every time.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, FirstFragment.newInstance());
fragmentTransaction.commit();
bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.menu_dialer:
fragment = FirstFragment.newInstance();
break;
case R.id.menu_email:
fragment = SecondFragment.newInstance();
break;
case R.id.menu_map:
fragment = ThirdFragment.newInstance();
break;
}
if (fragment != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit();
}
return true;
}
});
I also tried the onAttach and Deattach solutions but again no success.
VIDEO LINK : new i tried Nino Handler version it only works when i tap on the same fragment button
Maybe it is connected that i am using canary version or something wrong in my gradle dependencies?
NEW UPDATES:
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private static final String TAG_FRAGMENT_ONE = "fragment_one";
private static final String TAG_FRAGMENT_TWO = "fragment_two";
private FragmentManager fragmentManager;
private Fragment currentFragment;
String TAG = "babken";
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
Fragment fragment = null;
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_ONE);
if (fragment == null) {
fragment = FragmentFirst.newInstance();
}
replaceFragment(fragment, TAG_FRAGMENT_ONE);
break;
case R.id.navigation_dashboard:
fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_TWO);
if (fragment == null) {
fragment = FragmentSecond.newInstance();
}
replaceFragment(fragment, TAG_FRAGMENT_TWO);
break;
}
return true;
}
};
private void replaceFragment(#NonNull Fragment fragment, #NonNull String tag) {
if (!fragment.equals(currentFragment)) {
fragmentManager
.beginTransaction()
.replace(R.id.armen, fragment, tag)
.commit();
currentFragment = fragment;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_ONE);
if (fragment == null) {
fragment = FragmentFirst.newInstance();
}
replaceFragment(fragment, TAG_FRAGMENT_ONE);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
I had similar issue, but this code solved my problem.
public class MainActivity extends AppCompatActivity {
boolean doubleBackToExitPressedOnce = false;
final Fragment fragment1 = new HomeFragment();
final Fragment fragment2 = new DashboardFragment();
final Fragment fragment3 = new NotificationsFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment1;
BottomNavigationView navigation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
setFragment(fragment1, "1", 0);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
setFragment(fragment1, "1", 0);
return true;
case R.id.navigation_dashboard:
setFragment(fragment2, "2", 1);
return true;
case R.id.navigation_notifications:
setFragment(fragment3, "3", 2);
return true;
}
return false;
}
};
public void setFragment(Fragment fragment, String tag, int position) {
if (fragment.isAdded()) {
fm.beginTransaction().hide(active).show(fragment).commit();
} else {
fm.beginTransaction().add(R.id.main_container, fragment, tag).commit();
}
navigation.getMenu().getItem(position).setChecked(true);
active = fragment;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
if (active == fragment1) {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
} else {
setFragment(fragment1, "1", 0);
}
}
}
I wouldn't keep the fragment instances globally.
Instead add a tag to the fragment when creating them
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, new PlaceholderFragment(), TAG_PLACEHOLDER)
.commit();
Then you can always retrieve it like this:
Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_PLACEHOLDER);
if (fragment == null) {
fragment = new PlaceholderFragment();
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, fragment, TAG_PLACEHOLDER)
.commit();
UPDATE: I updated my answer and to provide a complete solution:
private static final String TAG_FRAGMENT_ONE = "fragment_one";
private static final String TAG_FRAGMENT_TWO = "fragment_two";
private static final String TAG_FRAGMENT_THREE = "fragment_three";
private FragmentManager fragmentManager;
private Fragment currentFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// instantiate the fragment manager
fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_ONE);
if (fragment == null) {
fragment = FirstFragment.newInstance();
}
replaceFragment(fragment, TAG_FRAGMENT_ONE);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.menu_dialer:
// I'm aware that this code can be optimized by a method which accepts a class definition and returns the proper fragment
Fragment fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_ONE);
if (fragment == null) {
fragment = FirstFragment.newInstance();
}
replaceFragment(fragment, TAG_FRAGMENT_ONE);
break;
case R.id.menu_email:
Fragment fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_TWO);
if (fragment == null) {
fragment = SecondFragment.newInstance();
}
replaceFragment(fragment, TAG_FRAGMENT_TWO);
break;
case R.id.menu_map:
Fragment fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_THREE);
if (fragment == null) {
fragment = ThirdFragment.newInstance();
}
replaceFragment(fragment, TAG_FRAGMENT_THREE);
break;
}
return true;
}
});
}
private void replaceFragment(#NonNull Fragment fragment, #NonNull String tag) {
if (!fragment.equals(currentFragment)) {
fragmentManager
.beginTransaction()
.replace(R.id.frameLayout, fragment, tag)
.commit();
currentFragment = fragment;
}
}
ADDITIONAL INFO: If you want to be sure that the fragment states don't change and if you also want to be able to swipe the fragments you should consider using a ViewPager with a FragmentStatePagerAdapter and change the current fragment in the adapter with every click event
I wrote a Kotlin Extension function for FragmentManager class
fun FragmentManager.switch(containerId: Int, newFrag: Fragment, tag: String) {
var current = findFragmentByTag(tag)
beginTransaction()
.apply {
//Hide the current fragment
primaryNavigationFragment?.let { hide(it) }
//Check if current fragment exists in fragmentManager
if (current == null) {
current = newFrag
add(containerId, current!!, tag)
} else {
show(current!!)
}
}
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.setPrimaryNavigationFragment(current)
.setReorderingAllowed(true)
.commitNowAllowingStateLoss()
}
This can be called by supportFragmentManager.swtich(R.id.container,newFrag,newFrag.TAG) from onNavigationItemSelected
All the previous answers are using fragmentTransaction.replace(...). This will replace the current fragment by destroying it (which is causing the problem). Therefore all those solutions won't actually work.
This is the closest thing I could get to as a solution for this problem:
private void selectContentFragment(Fragment fragmentToSelect)
{
FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();
if (this.getSupportFragmentManager().getFragments().contains(fragmentToSelect)) {
// Iterate through all cached fragments.
for (Fragment cachedFragment : this.getSupportFragmentManager().getFragments()) {
if (cachedFragment != fragmentToSelect) {
// Hide the fragments that are not the one being selected.
fragmentTransaction.hide(cachedFragment);
}
}
// Show the fragment that we want to be selected.
fragmentTransaction.show(fragmentToSelect);
} else {
// The fragment to be selected does not (yet) exist in the fragment manager, add it.
fragmentTransaction.add(R.id.fragment_container, fragmentToSelect);
}
fragmentTransaction.commit();
}
To make this work, you should keep track of the fragments in an array (or in separate variables) in your Activity. I for reference pre-instantiated all fragments in a SparseArray.
You can use attach() and detach() methods:
private FirstFragment firstFragment = FirstFragment.newInstance();
private SecondFragment secondFragment= SecondFragment.newInstance();
private ThirdFragment thirdFragment = ThirdFragment.newInstance();
navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_dialer:
changeFragment(firstFragment, "firstFragment");
return true;
case R.id.menu_email:
changeFragment(secondFragment, "secondFragment");
return true;
case R.id.menu_map:
changeFragment(thirdFragment, "thirdFragment");
return true;
}
return false;
}
});
public void changeFragment(Fragment fragment, String tagFragmentName) {
FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
Fragment currentFragment = mFragmentManager.getPrimaryNavigationFragment();
if (currentFragment != null) {
fragmentTransaction.detach(currentFragment);
}
Fragment fragmentTemp = mFragmentManager.findFragmentByTag(tagFragmentName);
if (fragmentTemp == null) {
fragmentTemp = fragment;
fragmentTransaction.add(R.id.content, fragmentTemp, tagFragmentName);
} else {
fragmentTransaction.attach(fragmentTemp);
}
fragmentTransaction.setPrimaryNavigationFragment(fragmentTemp);
fragmentTransaction.setReorderingAllowed(true);
fragmentTransaction.commitNowAllowingStateLoss();
}
So I have been researching for this for a long time and figured out that there is no way to reuse fragment with AUTOMATICALLY saved states you have to save your needed states manually then retrieve them whenever a new fragment is created but what about scroll position it is too hard and even in some cases it is impossible to save scroll view position state(e.g. recycler view). So i used the concept called VISIBILITY when i click on the button that fragment becomes visible others hide automatically.
How about that.
You declare the fragments in the class.
Fragment firstFragment,secondFragment,thirdFragment;
then in the switch-case you can code as that:
switch (item.getItemId()) {
case R.id.menu_dialer:
if(firstFragment != null) {
fragment = firstFragment;
}else{
fragment = FirstFragment.newInstance();
}
break;
case R.id.menu_email:
// the same ...
break;
case R.id.menu_map:
//the same ...
break;
}
Thomas's answer nearly helped me but had an issue that whenever I open new fragments the very first time, they overlapped but doesn't get overlapped once I open them again by pressing the menu buttons.
So I modified his code and obtained the solution using the following code:
private fun selectContentFragment(fragmentToSelect: Fragment) {
val fragmentTransaction = fragmentManager?.beginTransaction()
if (fragmentManager?.fragments?.contains(fragmentToSelect)!!) {
// Show the fragment that we want to be selected.
fragmentTransaction?.show(fragmentToSelect)
} else {
// The fragment to be selected does not (yet) exist in the fragment manager, add it.
fragmentTransaction?.add(R.id.container, fragmentToSelect)
}
// Iterate through all cached fragments.
for (cachedFragment in fragmentManager?.fragments!!) {
if (cachedFragment !== fragmentToSelect) {
// Hide the fragments that are not the one being selected.
// Uncomment following line and change the name of the fragment if your host isn't an activity and a fragment otherwise whole view will get hidden.
// if (!cachedFragment.toString().contains("HomeContainerFragment"))
fragmentTransaction?.hide(cachedFragment)
}
}
fragmentTransaction?.commit()
}
Make sure you're not passing the fragment's new instance every time.
This will work:
selectContentFragment(
when (item.itemId) {
R.id.home -> frag1
R.id.photoGallery -> frag2
else -> Home()
}
)
where frag1 and frag2 are global variables defined as:
val frag1 = Home()
val frag2 = PhotoGallery()
This will not work:
selectContentFragment(
when (item.itemId) {
R.id.home -> Home()
R.id.photoGallery -> PhotoGallery()
else -> Home()
}
)
It wasted my several hours. Hope it helps others!
**This code very helpful for me. Just like youtube. **
private Deque<Integer> fragmentIds = new ArrayDeque<>(3);
int itemId;
private HomeFragment homeFragment = new HomeFragment();
private FavouriteFragment favouriteFragment = new FavouriteFragment();
private NearmeFragment nearmeFragment = new NearmeFragment();
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentIds.push(R.id.action_home);
showTabWithoutAddingToBackStack(homeFragment);
bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemClicked);
}
private BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemClicked = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
itemId= item.getItemId();
if(fragmentIds.contains(itemId)){
fragmentIds.remove(itemId);
}
fragmentIds.push(itemId);
showTabWithoutAddingToBackStack(getFragment(item.getItemId()));
return true;
}
};
private Fragment getFragment(int fragmentId) {
switch (fragmentId) {
case R.id.action_home:
return homeFragment;
case R.id.action_favorites:
return favouriteFragment;
case R.id.action_nearme:
return nearmeFragment;
}
return homeFragment;
}
private void showTabWithoutAddingToBackStack(Fragment fragment) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment, fragment.getClass().getSimpleName()).commit();
}
#Override
public void onBackPressed() {
if(fragmentIds.getLast() != R.id.action_home){
fragmentIds.addLast(R.id.action_home);
}
fragmentIds.pop();
bottomNavigationView.getMenu().getItem(fragmentIds.size()-1).setChecked(true);
if (!fragmentIds.isEmpty()) {
showTabWithoutAddingToBackStack(getFragment(fragmentIds.peek()));
} else {
finish();
}
}
For Kotlin user may help this code :
First create a FragmentManager extension class
fun FragmentManager.replace(containerId: Int, fragment: Fragment, tag: String) {
var current = findFragmentByTag(tag)
beginTransaction()
.apply {
//Hide the current fragment
primaryNavigationFragment?.let { hide(it) }
//Check if current fragment exists in fragmentManager
if (current == null) {
current = fragment
add(containerId, current!!, tag)
} else {
show(current!!)
}
}
.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK)
.setPrimaryNavigationFragment(current)
.setReorderingAllowed(true)
.commitNowAllowingStateLoss()
}
Now simply call it on your
onNavigationItemSelected
supportFragmentManager.replace(R.id.fragment_id,YourFragmentClass,yourFragment.TAG)
I had the same issue but I resolved it by creating multiple fragment hosts then I added fragments in fragmentHost. And when bottom nav itemSelected I just make the required host fragment visible and other host fragmentsHost to visibility gone.
This method may be wrong but it works perfectly for me.
And we don't have to manually handle the states of fragment only we need to handle backpress of activity.
But this method does not pause the fragments.
And I don't know how to handle the pauses and resumes of fragments so please reply to me.
Maybe This will help you.
This is my fragmentHosts Home Activity:
<androidx.fragment.app.FragmentContainerView
android:id="#+id/homeFragmentHost"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottom_NavBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/libraryFragmentHost"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottom_NavBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/myStuffFragmentHost"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottom_NavBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/moreFragmentHost"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottom_NavBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_NavBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/app_bottom_nav_menu"
tools:ignore="BottomAppBar" />
Home Activity onCreate:
supportFragmentManager.beginTransaction().replace(R.id.homeFragmentHost,HomeFragment()).commitNow()
supportFragmentManager.beginTransaction().replace(R.id.libraryFragmentHost,LibraryFragment()).commitNow()
supportFragmentManager.beginTransaction().replace(R.id.myStuffFragmentHost,MyStuffFragment()).commitNow()
supportFragmentManager.beginTransaction().replace(R.id.moreFragmentHost,MoreFragment()).commitNow()
homeFragmentHost.visibility = View.VISIBLE
Bottom nav Item Selected listener
override fun onNavigationItemSelected(item: MenuItem): Boolean {
return when(item.itemId){
R.id.homeFragment -> loadFragmentHost(homeFragmentHost)
R.id.libraryFragment -> loadFragmentHost(libraryFragmentHost)
R.id.myStuffFragment -> loadFragmentHost(myStuffFragmentHost)
R.id.moreFragment -> loadFragmentHost(moreFragmentHost)
else -> false
}
loadFragmentHost function
private fun loadFragmentHost(view:FragmentContainerView): Boolean {
val list = arrayListOf(homeFragmentHost,libraryFragmentHost,myStuffFragmentHost,moreFragmentHost)
list.remove(view)
view.visibility = View.VISIBLE
list.forEach {
it.visibility = View.GONE
}
return true
}
public class MainActivity extends AppCompatActivity {
boolean doubleBackToExitPressedOnce = false;
final Fragment fragment1 = new HomeFragment();
final Fragment fragment2 = new DashboardFragment();
final Fragment fragment3 = new NotificationsFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment1;
BottomNavigationView navigation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
setFragment(fragment1, "1", 0);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
setFragment(fragment1, "1", 0);
return true;
case R.id.navigation_dashboard:
setFragment(fragment2, "2", 1);
return true;
case R.id.navigation_notifications:
setFragment(fragment3, "3", 2);
return true;
}
return false;
}
};
public void setFragment(Fragment fragment, String tag, int position) {
if (fragment.isAdded()) {
fm.beginTransaction().hide(active).show(fragment).commit();
} else {
fm.beginTransaction().add(R.id.main_container, fragment, tag).commit();
}
navigation.getMenu().getItem(position).setChecked(true);
active = fragment;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
if (active == fragment1) {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
} else {
setFragment(fragment1, "1", 0);
}
}
}
Maintain Bottom sheet fragment Reusable
BackPress Maintain
double back press to exit
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
Toaster toaster;
private final Fragment androidFragment = new AndroidFragment();
private final Fragment settingsFragment = new SettingsFragment();
Fragment active;
String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toaster = new Toaster(this);
bottomNavigationView = findViewById(R.id.bottom_navigation);
renderFragment(androidFragment, "android");
active = androidFragment;
bottomNavigationView.setOnItemSelectedListener(
item -> {
Log.e(TAG, "onCreate: " + active );
if(item.getItemId() == R.id.action_android){
renderFragment(androidFragment, "android");
return true;
}
else if(item.getItemId() == R.id.action_settings){
renderFragment(settingsFragment, "settings");
return true;
}
return false;
}
);
}
private void renderFragment(Fragment fragment, String tag){
FragmentManager fragmentManager = getSupportFragmentManager();
if(fragment.isAdded()){
fragmentManager.beginTransaction().hide(active).show(fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
}
else {
if(active != null){
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container_view, fragment, tag)
.hide(active)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
}
else {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container_view, fragment, tag)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
}
}
active = fragment;
}
}
This worked for me Guys.
Create the three fragments as members of the class and reuse them.
public class MainActivity extends AppCompatActivity {
private final Fragment mFirstFragment = new FirstFragment();
private final Fragment mSecondFragment = new SecondFragment();
private final Fragment mThirdFragment = new ThirdFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
......
......
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, mFirstFragment);
fragmentTransaction.commit();
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (bottomNavigationView.getSelectedItemId() != item.getItemId()) {
switch (item.getItemId()) {
R.id.menu_dialer:
replaceFragment(mFirstFragment);
break;
case R.id.menu_email:
replaceFragment(mSecondFragment);
break;
case R.id.menu_map:
replaceFragment(mThirdFragment);
break;
}
}
return true;
}
});
}
private void replaceFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit();
}
}
When my app starts up, it replaces layout with fragment, that needs AsyncTask to load correctly. When I load this fragment from navigation drawer, everything works fine, but wen it loads on app startup, AsyncTask doesn't execute. How can i fix this?
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Fragment fragment = null;
Class fragmentClass = null;
fragmentClass = NewsFragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayoutForFragments, fragment);
fragmentTransaction.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(0).setChecked(true);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
//Wybory elementów w navigationdrawer
Fragment fragment = null;
Class fragmentClass = null;
int id = item.getItemId();
if (id == R.id.nav_news) {
fragmentClass = NewsFragment.class;
} else if (id == R.id.nav_map) {
fragmentClass = MapFragment.class;
} else if (id == R.id.nav_buildings) {
fragmentClass = BuildingsFragment.class;
} else if (id == R.id.nav_manage) {
fragmentClass = MapFragment.class;
} else if (id == R.id.nav_info) {
fragmentClass = AppInfoFragment.class;
} else if (id == R.id.nav_bugreport) {
fragmentClass = ContactFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frameLayoutForFragments, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Fragment that loads on startup:
public class NewsFragment extends Fragment {
public static ArrayList <ParsedWebData> list = new ArrayList<ParsedWebData>();
public NewsFragment() {
// Required empty public constructor
}
public static NewsFragment newInstance(Context context) {
NewsFragment fragment = new NewsFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Start AsyncTask w momencie ładowania fragmentu
AsyncXMLParser parser = new AsyncXMLParser();
parser.execute();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_news, container, false);
ListView listView = (ListView) view.findViewById(R.id.listViewNews);
CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(getContext(), R.id.listViewNews, list);
listView.setAdapter(customListViewAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ParsedWebData singleData = list.get(position);
String url = singleData.getUrl();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
});
return view;
}
}
AsyncTask:
public class AsyncXMLParser extends AsyncTask <Void, Integer, ArrayList<ParsedWebData>> {
#Override
protected void onPreExecute() {
}
#Override
protected ArrayList<ParsedWebData> doInBackground(Void... params) {
ArrayList<ParsedWebData> list = new ArrayList<ParsedWebData>();
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
InputStream input = new URL("linkhere").openStream();
xpp.setInput(input, "UTF_8");
int eventType = xpp.getEventType();
String text = null;
ParsedWebData data = new ParsedWebData();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = xpp.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("item")) {
data = new ParsedWebData();
}
break;
case XmlPullParser.TEXT:
text = xpp.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("item")) {
// add employee object to list
list.add(data);
} else if (tagname.equalsIgnoreCase("title")) {
data.title = text;
} else if (tagname.equalsIgnoreCase("link")) {
data.url = text;
} else if (tagname.equalsIgnoreCase("description")) {
text = Jsoup.parse(text).text();
data.description = text;
}
break;
default:
break;
}
eventType = xpp.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
NewsFragment.list = list;
return list;
}
#Override
protected void onPostExecute(ArrayList <ParsedWebData> result) {
}
}
I tried to find the reason myself, and tried to execute AsyncTask manually in MainActivity, but it didn't help. Any ideas what's wrong?
What's wrong is, that the AsyncTask (thread) finishes after your OnCreateView runs, so you don't see your data.
What you could do, is move the line of code NewsFragment.list = list; from doInBackground(), to onPostExecute(), and call adapter.notifyDataSetChanged() after that. But the issue is that in your design you have no access to listView adapter in AsyncTask
In addition, having the list as a static variable in the Fragment, so that you can access it from the AsyncTask is very bad programming design.
You should remove the static list variable, and should redesign your AsyncTask, that you pass the listView to the constructor (you will have to move it from onCreate to onCreateView), and assign the listView to a member in the AsyncTask. Then in onPostExecute set the adapter (not in onCreateView)
public class AsyncXMLParser extends AsyncTask <Void, Integer, ArrayList<ParsedWebData>> {
CustomListViewAdapter _adapter;
Public AsyncXMLParser(CustomListViewAdapter adapter) {
_adapter = adapter;
}
...
#Override
protected void onPostExecute(ArrayList <ParsedWebData> result) {
CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(getContext(), R.id.listViewNews, result);
listView.setAdapter(customListViewAdapter);
// Or, depending on design
_adapter.notifyDataSetChanged();
}
}
This way, the list is not static variable (you can remove code from the fragment), and the listView is filled only after the AsyncTask is finished. If you later need access to the actual list data, you can get it from the Adapter.
I couldn't find the reason to why Asynctask isnt being executed but depending on how much background work you want to do, you might not want to use
Asynctask. Asynctask should only be used to do operations that take a small amount of time(at most seconds) since its lifetime is tied to the lifetime of a ui compoment. The issue here could very well be that you're killing the asynctask because its connected to the ui.
I would recommend you to use an intentservice. They are really easy to use and can run as long as you like in the background. Since u can't seem to find the issue with Asynctask in this case you would probably save time by using intentservices instead. https://developer.android.com/reference/android/app/IntentService.html
I am getting the error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
when trying to set the text of a textView within a fragment (in the method changeText)
Here is my fragment code
public class MainFragment extends Fragment {
String date;
TextView infoText2;
public MainFragment() {
// Required empty public constructor
}
public Boolean daily;
//instantiate variables
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.fragment_main, container, false);
infoText2 = (TextView) rootview.findViewById(R.id.infoText2);
//TODO THIS COULD POSSIBLY BE A PROBLEM TOO
//calls setdate function
startup(rootview);
btnClick(rootview);
return rootview;
}
public void startup(View v) {
date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
//gets the current date
TextView textView = (TextView) v.findViewById(R.id.infoText);
textView.setText(date);
//finds and replaces text in textView
SeekBar seekBar = (SeekBar) v.findViewById(R.id.seekBar2);
seekBar.setProgress(5);
seekBar.setMax(10);
//sets the seekbar progress
//BEGIN CHECKING IF USER HAS ADDED ENTRY
//TODO ADD IF ENTRY NOT COMPLETED STATEMENT
}
public void changeText(String mText) {
//TODO FIX THIS PROBLEM
infoText2.setText(mText);
//runs getTheData method
}
public void btnClick(View v) {
Button clickButton = (Button) v.findViewById(R.id.btnRate);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainFragment.this.getActivity(), "Rating successful", Toast.LENGTH_SHORT).show();
//conformation toast
SeekBar seekBar = (SeekBar) MainFragment.this.getActivity().findViewById(R.id.seekBar2);
seekBar.setEnabled(false);
Button btnRate = (Button) MainFragment.this.getActivity().findViewById(R.id.btnRate);
btnRate.setEnabled(false);
//disable seekbar and button
daily = true;
//marks rating complete for the day
//TODO reset complete back to false the next day
int value = seekBar.getProgress();
TextView ratingDisplay = (TextView) MainFragment.this.getActivity().findViewById(R.id.ratingText);
ratingDisplay.setText("Rating: " + Integer.toString(value) + " out of 10");
//sets the rating display to the value of the seekbar
((MainActivity) getActivity()).writeText(date, Integer.toString(value));
//Runs writeText method
((MainActivity) getActivity()).getTheData();
}
});
and also here is the code from my MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = null;
Toolbar toolbar = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//sets the initial fragment
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
//sets up the navigation drawer
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
/*public void rateOnClickListener(){
Button btnNavigator = (Button)findViewById(R.id.btnRate);
btnNavigator.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MainFragment fragment = new MainFragment();
MainFragment.rate(v);
Toast.makeText(MainActivity.this, "yes", Toast.LENGTH_LONG);
}
});
}*/
//if the user presses the back button when the navdrawer is open
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_gallery) {
SecondFragment fragment = new SecondFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void writeText(String timestamp, String rating) {
String my_results = rating + ", " + timestamp + "\n";
// String my results contains the date/time and the my_total score
String file_name = "fresh_3";
try {
FileOutputStream fileOutputStream = openFileOutput(file_name, MODE_APPEND | MODE_PRIVATE);
fileOutputStream.write(my_results.getBytes());
fileOutputStream.close();
Toast.makeText(getApplicationContext(), my_results + " success", Toast.LENGTH_LONG).show();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
//saves values to file
}
public void getTheData() {
try {
String Message;
String myData;
int i = 0;
FileInputStream fileInputStream = openFileInput("fresh_3");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
while ((Message = bufferedReader.readLine()) != null) {
stringBuilder.append(Message + "\n");
i++; //counter for the array to sort the data
}
//put data in a string with lines
fileInputStream.close();
myData = stringBuilder.toString(); // old successful method to output - need to count the lines
//instantiate the array
String mylines[] = new String[i]; //assigning number of lines to new array as Java cannot make a indeterminate array
mylines = myData.split("\\n");
Arrays.sort(mylines, Collections.reverseOrder()); //sort the array in order of first character - reverse order - descending
//remove the dodgy brackets and print the sorted data
MainFragment mainFragment = new MainFragment();
mainFragment.changeText(Arrays.toString(mylines).replaceAll("\\[|\\]", "")); //write the sorted data using regex (regular expression)
//infoText2.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Number of records " + i, Toast.LENGTH_LONG).show(); //shows line number
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
}
I have tried all sorts of things to fix this including getActivity() in front of the findViewById and also rootview.. I have re-arranged them in different positions but nothing has worked.
It appears that everything apart from the .setText is working but I would very much appreciate it if somebody could tell me why it refuses to cooperate.
The fragment is not attached to the activity when onCreateView runs. To make sure it works, call the needed functions in the OnResume() of the fragment.
For example:
gridview_left = (GridView)getView().findViewById(R.id.gridview_left);
Make sure that TextView with #+id/infoText2 exists in fragment_main layout file.
Try shifting the btnClick(rootview) call inside the onActivityCreated()...
I think the fragment is not yet attached to the activity
MainFragment mainFragment = new MainFragment();
mainFragment.changeText(Arrays.toString(mylines).replaceAll("\\[|\\]", ""));
None of the fragment lifecycle methods such as onCreateView() have been run when you're invoking a method on the fragment here. You'd need to wait for your fragment transaction to be executed.
A better idea would be to use the arguments Bundle to pass parameters to your fragment, and read the arguments in the fragment itself in a lifecycle method.
Hello everyone I have a question on rotating fragments and restoring them after detaching them. Currently I have three fragments: Fragment_Data, Fragment_Log, and Fragment_Control. My problem is if I rotate Fragment_Data, then detach and add Fragment_Log, then detach Fragment_Log and attempt to attach Fragment Data it fails to reattach. If I keep the device vertical and repeat the same steps the fragments don't have any issues reattaching. I try to detach, add, and attach the fragments in my PanelManager method.
What exactly is going one during the lifecycle of the Fragment_Data that causes issues when trying to reattach?
MainActivty.java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, FragmentControl.ButtonSetListener{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentControlTransction = fragmentManager.beginTransaction();
FragmentTransaction fragmentDataTransaction = fragmentManager.beginTransaction();
FragmentControl fragmentControl = new FragmentControl();
FragmentData fragmentData = new FragmentData();
FragmentLog fragmentLog = new FragmentLog();
FragmentGraph fragmentGraph = new FragmentGraph();
//Create Fragment Manager
FragmentManager panelManager = getFragmentManager();
//Boolean values for panels
boolean data_panel = true; //True because it is the first panel created in the View; id = 1
boolean log_panel = false; //id = 2
boolean graph_panel = false; //id = 3
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if(savedInstanceState != null){
return;
}
//Generate Fragment_Data and Fragment_Control Panels
//These are the first two panels introduced into the app
fragmentControlTransction.add(R.id.fragment_control_panel, fragmentControl, "control");
fragmentControlTransction.commit();
fragmentDataTransaction.add(R.id.fragment_data_panel, fragmentData, "data");
fragmentDataTransaction.commit();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.data_log) {
PanelManager(2);
} else if (id == R.id.data_graph) {
PanelManager(3);
} else if (id == R.id.nav_share) {
PanelManager(1);
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void PanelManager(int panel_id){
if(panel_id == 1){
//detach any panel attached to the fragment_data_panel
panelManager.beginTransaction().detach(getFragmentManager().findFragmentById(R.id.fragment_data_panel)).commit();
//Switch to data panel since it already exists
panelManager.beginTransaction().attach(fragmentData).commit();
} else if(panel_id == 2){
//detach any panel attached to the fragment_data_panel
panelManager.beginTransaction().detach(getFragmentManager().findFragmentById(R.id.fragment_data_panel)).commit();
//Switch to log panel
if(log_panel == false){
//If log panel is false, create log_panel for the first time
log_panel = true;
panelManager.beginTransaction().add(R.id.fragment_data_panel, fragmentLog).commit();
} else {
//Log_panel exists, so just attach fragment back
panelManager.beginTransaction().attach(fragmentLog).commit();
}
} else if(panel_id == 3){
//detach any panel attached to the fragment_data_panel
panelManager.beginTransaction().detach(getFragmentManager().findFragmentById(R.id.fragment_data_panel)).commit();
//Switch to graph panel
if(graph_panel == false){
//If log panel is false, create graph_panel for the first time
graph_panel = true;
panelManager.beginTransaction().add(R.id.fragment_data_panel, fragmentGraph).commit();
} else {
//Log_panel exists, so just attach fragment back
panelManager.beginTransaction().attach(fragmentGraph).commit();
}
}
}
//Call updateList method in FragmentLog to update ListView
#Override
public void app_log_update(String data, int icon) {
FragmentLog fragmentLog = (FragmentLog)getFragmentManager().findFragmentById(R.id.fragment_data_panel);
fragmentLog.updateList(data, icon);
}
}
FragmentData.java
public class FragmentData extends Fragment implements View.OnClickListener{
public EditText message_text;
public TextView display_message;
public Button button;
public String return_message = null;
boolean has_text_entered = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
return_message = savedInstanceState.getCharSequence("savedText").toString();
}
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_data, container, false);
button = (Button)view.findViewById(R.id.set_text_button);
button.setOnClickListener(this);
display_message = (TextView) view.findViewById(R.id.display_message);
display_message.setText("This is a temp statement");
if(return_message != null){
display_message = (TextView) view.findViewById(R.id.display_message);
display_message.setText(return_message);
}
return view;
}
#Override
public void onClick(View v) {
has_text_entered = true;
message_text = (EditText)getActivity().findViewById(R.id.text_message);
String message = message_text.getText().toString();
display_message = (TextView)getActivity().findViewById(R.id.display_message);
display_message.setText(message);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
TextView text = (TextView)getActivity().findViewById(R.id.display_message);
CharSequence userText = text.getText();
if(userText != null){outState.putCharSequence("savedText", userText);}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
}
Thank you for the help everyone!
I've implemented the following code , but when I run the app and click on an item in Drawer nothing happened. I guess it's a problem related to the Listener but I couldn't grasp how to fix it. or maybe other problem!
once I click on one item the only thing happen is the following two lines in the logcat :
01-12 03:14:26.606 1565-1565/? I/LatinIME: Starting input. Cursor position = -1,-1
01-12 03:14:29.601 1565-1565/? I/LatinIME: Starting input. Cursor position = 0,0
here is the code:
public class Welcome extends AppCompatActivity {
private DrawerLayout mDrawer,dlDrawer;
private NavigationView nvDrawer;
private ActionBarDrawerToggle drawerToggle;
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Find our drawer view
dlDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = setupDrawerToggle();
// Tie DrawerLayout events to the ActionBarToggle
dlDrawer.setDrawerListener(drawerToggle);
nvDrawer = (NavigationView) findViewById(R.id.nvView);
// Setup drawer view
setupDrawerContent(nvDrawer);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.drawer_layout, new EditPersonal());
tx.commit();
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
private ActionBarDrawerToggle setupDrawerToggle() {
return new ActionBarDrawerToggle(this, dlDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the planet to show based on
// position
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_first_fragment:
fragmentClass = EditPersonal.class;
break;
case R.id.nav_second_fragment:
fragmentClass = Chronogram.class;
break;
case R.id.nav_third_fragment:
fragmentClass = BloodTest.class;
break;
case R.id.nav_forth_fragment:
fragmentClass = GlucoseTest.class;
break;
case R.id.nav_fifth_fragment:
fragmentClass = SendRecord.class;
break;
case R.id.nav_sixth_fragment:
fragmentClass = ReviewLogin.class;
break;
case R.id.nav_seventh_fragment:
fragmentClass = SignOut.class;
break;
default:
fragmentClass = EditPersonal.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent,fragment).commit();
// Highlight the selected item, update the title, and close the drawer
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawer.closeDrawers();
}
// ...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
// Make sure this is the method with just `Bundle` as the signature
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
drawerToggle.onConfigurationChanged(newConfig);
}
}
the app screen shot:
On your switch case, try something like this:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
case R.id.nav_first_fragment:
EditPersonal editPersonal= new EditPersonal();
fragmentTransaction.replace(R.id.flContent, editPersonal);
fragmentTransaction.commit();
break;
Do this on other cases too.
Have you tried putting logs into your code to see where the logic flows to or fails to flow to?
For example inside your switch statement, try seeing if menuItem.getItemId() actually matches your case statements. It'd help to see if your listener is actually the problem or if it's just not matching the cases. Or it could be a problem with your fragment manager.
Check out http://developer.android.com/reference/android/util/Log.html. You should be able to see the logs in logcat. Good luck!
Basically I have changed the code using the Activity (Navigation Drawer Activity) of Android studio itself. so here is the code :
public class Welcome extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.review_login_listview, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.menu_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if (id == R.id.nav_first_fragment) {
// Handle the camera action
}
else {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}