0

I've added an addToBackStack to a button, but when I hit the back button it goes back to the fragment it just exited, why? I've also tried to add remove() but that crashes the app. In an activity one can use finish() to do what I want, but what about in a fragment?

public class RateFrag extends Fragment implements View.OnClickListener {

private TextView title;
private RatingBar ratingBar;
private Button rateknap;
private Forslag forslag;

@Override
public View onCreateView(LayoutInflater i, ViewGroup container, Bundle savedInstanceState) {
    View rod = i.inflate(R.layout.activity_rate, container, false);

    title = (TextView) rod.findViewById(R.id.title);
    ratingBar = (RatingBar) rod.findViewById(R.id.ratingBar);
    rateknap = (Button) rod.findViewById(R.id.rateknap);
    rateknap.setOnClickListener(this);

    forslag = ((AnnonceDisplay) this.getActivity()).getForslag();
    title.setText(forslag.getTitle());


    return rod;
}

@Override
public void onClick(View v) {
    if (v == rateknap) {
        AlertDialog alert = new AlertDialog.Builder(getActivity())
                .setMessage("Vil du give " + ratingBar.getRating() + " stjerner?")
                .setPositiveButton("Ja", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                launchintent();


                            }
                        }
                )
                .setNegativeButton("Nej", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();

                            }
                        }
                ).show();


    }
}

private void launchintent() {
    Fragment fragment = new AnnonceFrag();
    getFragmentManager().beginTransaction()
            .replace(R.id.fragment, fragment)
            .remove(this)
            .addToBackStack(null)
            .commit();


 }
}

I get these errors:

12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime: FATAL EXCEPTION: main
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime: Process: com.example.hadi.do2get, PID: 21875
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime: java.lang.IllegalStateException: Fragment already added: RateFrag{1737948 #2 id=0x7f0c0069}
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1138)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:1597)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1504)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:499)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.app.Activity.onBackPressed(Activity.java:2482)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.app.Activity.onKeyUp(Activity.java:2456)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.KeyEvent.dispatch(KeyEvent.java:2643)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.app.Activity.dispatchKeyEvent(Activity.java:2707)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2276)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4020)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3982)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3713)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3874)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2208)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1849)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1840)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2185)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.os.MessageQueue.nativePollOnce(Native Method)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.os.MessageQueue.next(MessageQueue.java:143)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:122)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5257)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
12-04 00:21:26.050 21875-21875/com.example.hadi.do2get E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Hudhud
  • 61
  • 1
  • 10

0 Answers0