When I pause the app for a few moments the activity's Fragmnetos are recreated
public class CopycatActivity extends FragmentActivity implements CopycatInterface {
public void flipFragment(int type) {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
switch (type) {
case 1:
ft.replace(R.id.container, zWaitFragment);
break;
case 2:
ft.replace(R.id.container, zMasterMode);
break;
case 3:
ft.replace(R.id.container, zCopyMode);
break;
case 4:
ft.replace(R.id.container, zRateDraws);
break;
case 5:
ft.replace(R.id.container, zWinnerFragment);
break;
case 6:
ft.replace(R.id.container, zSleepMode);
break;
}
ft.commitAllowingStateLoss();
}
}
Depending on which fragment I am in, when paused it is reset
Related
I tried many methods to save instances but I am confused how to save instances in backstack because no one described how to use it if there multiple fragments.my code in mainactivity below:
bottomNavigationView.setOnItemSelectedListener(item -> {
switch (item.getItemId()){
case R.id.home:
loadFragment(new HomeFragment(),0);
break;
case R.id.create:
loadFragment(new CreateFragment(),1);
break;
case R.id.profile:
loadFragment(new ProfileFragment(),1);
break;
}
return true;
});
public void loadFragment(Fragment fragment,int flag){
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
if(flag==0){
fragmentTransaction.add(R.id.frame_layout,fragment);
fragmentManager.popBackStack(ROOT_FRAGMENT_TAG,FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentTransaction.addToBackStack(ROOT_FRAGMENT_TAG);
}
else {
fragmentTransaction.replace(R.id.frame_layout,fragment);
fragmentTransaction.addToBackStack(null);
}
fragmentTransaction.commit();
}
I suggest you to make your Fragment Classes Singleton.
private static MyFragment fragment;
public static MyFragment getInstance() {
if (fragment == null) {
fragment = new MyFragment();
}
return fragment;
}
then use getInstance() method to instantiate your fragment
So here's the code:
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new DoctorFragment();
break;
case 2:
fragment = new HospitalFragment();
break;
case 3:
fragment = new SpecialClinicFragment();
break;
case 4:
fragment = new PharmacyFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
//the parameter "fragment" in this line of code gives an error message.
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
getActionBar().setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
It seems like I haven't properly initialised fragment instance.
The error message was "Wrong 2nd argument type" but I'm pretty sure I put in the right parameter.
Problem is not with the argument.You have used Fragment from
android.support.v4 library but you are making transaction on android.app.fragment.
Solution:
import android.app.fragment instead of android.support.v4.fragment
Because you cannot mix up with support.v4 library and android.app.*
I am using an Android material navigation drawer for my app and am trying to create a log out button as one of the navigation drawer items
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
// Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AnotherFragment();
break;
case 1:
fragment = new AnotherFragment();
break;
case 2:
fragment = new AnotherFragment();
break;
case 3:
fragment = new AnotherFragment();
break;
case 4:
fragment = new AnotherFragment();
break;
case 5:
fragment = new AnotherFragment();
break;
case 6:
fragment = new AnotherFragment;
break;
case 7:
fragment = new AnotherFragment();
break;
case 8:
fragment = new AnotherFragment();
break;
case 9:
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
on case 9 I want to allow the user to log out,
Intent logOut = new Intent(GroupHomeActivity.this, LoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit();
logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
How can I allow the codes above to be executed on case 9?
Well here is what I think you should do :
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AnotherFragment();
break;
case 1:
fragment = new AnotherFragment();
break;
case 2:
fragment = new AnotherFragment();
break;
case 3:
fragment = new AnotherFragment();
break;
case 4:
fragment = new AnotherFragment();
break;
case 5:
fragment = new AnotherFragment();
break;
case 6:
fragment = new AnotherFragment;
break;
case 7:
fragment = new AnotherFragment();
break;
case 8:
fragment = new AnotherFragment();
break;
case 9:
fragment=null;
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
else {
Intent logOut = new Intent(GroupHomeActivity.this, ElgarLoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit();
logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
}
Simply because the return type is expecting a non null fragment. Well just return a null fragment, and on null logout the user.
Here is how I would handle the logic for the position 9 click
#Override
public void onNavigationDrawerItemSelected(int position) {
if(position == 9){
Intent logOut = new Intent(GroupHomeActivity.this, ElgarLoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit();
logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
}else{
switchFragment(position);
}
}
Here is how you would switch the fragments.
private void switchFragment(int position){
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AnotherFragment();
break;
case 1:
fragment = new AnotherFragment();
break;
case 2:
fragment = new AnotherFragment();
break;
case 3:
fragment = new AnotherFragment();
break;
case 4:
fragment = new AnotherFragment();
break;
case 5:
fragment = new AnotherFragment();
break;
case 6:
fragment = new AnotherFragment;
break;
case 7:
fragment = new AnotherFragment();
break;
case 8:
fragment = new AnotherFragment();
break;
default:
break;
}
if (fragment != null){
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
}
Just put your code after case 9 before break. Like this:
Case 9:
Intent logOut = new Intent(GroupHomeActivity.this, ElgarLoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit(); logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
StartActivity(logout);
break;
I have use .replace() for replace a old Fragment with a new, but in this moment i do no how use the same of .replace() by a Fragment to a mapsFragment.
Ideas?!
I can use replace? if i can' t use it, what I can use?
This is my code in this moment. (the .replace() don' t work)
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
mapsFragment fragM = null;
Fragment frag = null;
switch (position) {
case 0: //Home
mTitle = "Maps";
fragM = new mapsFragment();
break;
case 1: //Query utente
mTitle = "Section 2";
break;
case 2: //Query Amministratore
mTitle = "Section 3";
break;
}
if (position != 3) {
if(position==0){
fragmentManager.beginTransaction()
.replace(R.id.container, fragM)
.commit();
}else {
fragmentManager.beginTransaction()
.replace(R.id.container, frag)
.commit();
}
I have find the solution:link where i have found it
This is the code in particular:
if(position==0){
FragmentTransaction mTransaction = getSupportFragmentManager()
.beginTransaction();
SupportMapFragment mFRaFragment = new mapsFragment();
mTransaction.replace(R.id.container, mFRaFragment);
mTransaction.commit();
}else {
I have an activity called HomeActivity,
{
public class HomeActivity extends SherlockFragmentActivity
}
but the code below here
{
private void selectItem(int position) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Locate Position
switch (position) {
case 0:
ft.replace(R.id.content_frame, home);
break;
case 1:
ft.replace(R.id.content_frame, gadget);
break;
case 2:
ft.replace(R.id.content_frame, news);
break;
case 3:
ft.replace(R.id.content_frame, techno);
break;
case 4:
ft.replace(R.id.content_frame, game);
break;
case 5:
ft.replace(R.id.content_frame, tips);
break;
case 6:
ft.replace(R.id.content_frame, cgt);
break;
case 7:
ft.replace(R.id.content_frame, info);
break;
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
mDrawerList.setItemChecked(position, true);
// Close drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
the FragmentTransaction can't replace my fragment in SherlockFragmentActivity.
I have no idea with this, I search and implements some method from google and stackoverflow but my issue still not sot
please I need a hint with this..thanks
When working with compatibility frameworks like ActionBarSherlock and Google Support Library
it's easy to mix up imports with the native APIs.
Just take the FragmentTransaction class for example.
Make sure you are not importing the native version.
import android.app.FragmentTransaction;
Instead make sure you import the version from the support library.
import android.support.v4.app.FragmentTransaction;
Actually check all your imports that you use in your project making
sure you are picking the right ones (support.v4.app).