Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backstack android wear not working

I am trying to create an android wear app that only has one activity with one fragment inside of it. I want to be able to switch the fragments out based on button clicks or other user actions.

I can replace fragments just fine; however, when I swipe from left side of the screen to the right side of the screen. The app closes. I expected this swipe to act like a back button and not exit the app.

Main activity

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import  android.support.v4.app.FragmentTransaction;
import android.support.wearable.view.WatchViewStub;


public class MainActivity extends FragmentActivity implements FragmentChangeListener {

    int  fragmentContainerId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        final WatchViewStub stub = (WatchViewStub)findViewById(R.id.watch_view_stub);


        // instialize the fragment to the loading page
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub watchViewStub) {
                fragmentContainerId =  R.id.fragmentContainer;

                if (watchViewStub.findViewById(R.id.fragmentContainer) != null) {




                    Fragment1 fragment1= new Fragment1 ();

                    // In case this activity was started with special instructions from an Intent,
                    // pass the Intent's extras to the fragment as arguments
                    fragment1.setArguments(getIntent().getExtras());

                    // Add the fragment to the 'fragment_container'
                    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.add(R.id.fragmentContainer, fragment1).addToBackStack(null);
                    fragmentTransaction.commit();
                }
            }
        });
    }

    @Override
    public void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragmentContainer, fragment, fragment.toString()).addToBackStack(null);
        System.out.println("Entry count in backstack is: " + fragmentManager.getBackStackEntryCount());
        fragmentTransaction.commit();
    }
}

Fragment 1

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;



public class Fragment1 extends Fragment {


    public Fragment1() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment1,
                container, false);

        Button button1= (Button)view.findViewById(R.id.button1);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Fragment2 fragment2= new Fragment2 ();
                FragmentChangeListener fc=(FragmentChangeListener)getActivity();
                fc.replaceFragment(fragment2);
            }
        });
        return view;
    }
}

Fragment 2

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment2 extends Fragment {


    public Fragment2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v =inflater.inflate(R.layout.fragment2, container, false);

        return v;
    }


}

FragmentChangeListner

import android.support.v4.app.Fragment;


public interface FragmentChangeListener
{
    public void replaceFragment(Fragment fragment);
}

When I navigate from Fragment1 to Fragment2 via the button click. How can I navigate back to Fragment1 without exiting the app?

like image 500
Brian Thorpe Avatar asked Mar 12 '15 14:03

Brian Thorpe


People also ask

How to use back stack in Android OS?

Leave a Comment / Android Tutorial / Back Stack, Fragment Android OS provides a back stack function for Activity, it also provides the back stack function for Fragment. If you add one Fragment into the back stack, when you press the android device back menu, you can find the Fragment that is saved in the back stack popup.

How to use fragment back stack in Android?

If you add one Fragment into the back stack, when you press the android device back menu, you can find the Fragment that is saved in the back stack popup. Until all the saved Fragments in the back stack popup, then the activity will exit. 1. Fragment Back Stack Example. This example contains one activity and three fragments.

What is the back stack in Java?

The activities in the stack are only pushed onto and popped off the stack—pushed into the stack when the current activity starts it and popped off when the user exits it using the Back button. As a result, the back stack is an object structure that is “last in, first out.”

What is the back stack in Salesforce?

These activities are arranged in a stack—the back stack —in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages. When the user selects a message, a new activity opens to view that message. This new activity is added to the back stack.


1 Answers

One solution would be to wrap your second layout inside a

android.support.wearable.view.SwipeDismissFrameLayout

Then you can capture the dismiss gesture and inside the parent activity call getFragmentManager().popBackStack();

you can set listener on the SwipeDismissFrameLayout like so: swipeFrame.setDismissEnabled(true); swipeFrame.setOnDismissedListener(new SwipeDismissLayout.OnDismissedListener() { @Override public void onDismissed(SwipeDismissLayout swipeDismissLayout) { getActivity().getFragmentManager().popBackStack(); } });

like image 113
gdogaru Avatar answered Nov 14 '22 23:11

gdogaru