sockeqwe/SwipeBack

How could I add this into an Activity containing a content Fragment?

Closed this issue · 2 comments

public class ItemDescriptionActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, ItemDescriptionFragment.newInstance())
                    .commit();
        }
}

Your sample code shows how to add swipe back gesture to an Activity, however, it misses the part explaining Activity that contains Fragment.

It should work exactly the same way. But you probably define a FrameLayout with it's own id as Fragment container. I guess using android.R.id.content will cause problems, but I never tried this. Let me know if it works.

public class ItemDescriptionActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SwipeBack.attach(this, Position.LEFT)
            .setContentView(R.layout.activity_simple)
            .setSwipeBackView(R.layout.swipeback_default);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragmentContainer, ItemDescriptionFragment.newInstance(campaign))
                .commit();
    }
}

Actually android.R.id.content won't cause problem. But the code should be:

public class ItemDescriptionActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SwipeBack.attach(this, Position.LEFT)
            .setSwipeBackView(R.layout.swipeback_default);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, ItemDescriptionFragment.newInstance())
                .commit();
    }
}

SwipeBack's setContentView() call is not necessary because the Fragment provides the content. Calling this method would overlap two layouts.

Thank you for your quick response! I would suggest you to add this to your project wiki for Fragment support.