I had Created a Home activity which includes a Navigation Drawer onclick with fragment. I had included fragmentTransaction.addToBackStack(null).commit();
with the fragment transaction code to go back to previous page.
My Desired requirement was NavigationDrawer-->Fragment1-->Fragment2--> On BackButtonPress-->Fragment1(On BackButton Pressed)-->MainActivity.
But,Now i am only able to move to Fragment1
,But cant move to Fragment2
.
It shows an error java.lang.IllegalStateException: commit already called
In My MainActivity
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// Handle navigation view item clicks here.
int id = item.getItemId();
RelativeLayout mainLayout=(RelativeLayout)findViewById(R.id.mainlayout);
if (id == R.id.nav_project) {
ProjectFragment fragment = new ProjectFragment();
mainLayout.removeAllViews();
fragmentTransaction.add(R.id.mainlayout, fragment);
fragmentTransaction.addToBackStack(null).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return 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();
}
}
My First Fragment
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
/**
* A simple {@link Fragment} subclass.
*/
public class LoginFragment extends Fragment {
Button signin,login;
ImageView logo;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_login,container,false);
FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
final RelativeLayout mainLayout=(RelativeLayout)getActivity().findViewById(R.id.mainlayout);
signin=(Button)v.findViewById(R.id.button2);
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SignUpFragment fragment = new SignUpFragment();
mainLayout.removeAllViews();
fragmentTransaction.add(R.id.mainlayout, fragment);
fragmentTransaction.commit();
fragmentTransaction.addToBackStack(null).commit();
}
});
login=(Button)v.findViewById(R.id.button);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoginEnterFragment fragment = new LoginEnterFragment();
mainLayout.removeAllViews();
fragmentTransaction.add(R.id.mainlayout, fragment);
fragmentTransaction.commit();
fragmentTransaction.addToBackStack(null).commit();
}
});
logo=(ImageView)v.findViewById(R.id.imageView);
return v;
}
}
My third Fragment
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class LoginEnterFragment extends Fragment {
public LoginEnterFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_login_enter, container, false);
}
}
My Logcat
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: FATAL EXCEPTION: main
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: Process: com.example.rubin, PID: 676
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: java.lang.IllegalStateException: commit already called
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:621)
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at com.example.root5solutions.mirrealtors.LoginFragment$2.onClick(LoginFragment.java:53)
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at android.view.View.performClick(View.java:4780)
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19866)
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-01 23:44:17.237 676-676/com.example.rubin E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)