Update to follow fragment APIs.
Change-Id: If7f4d2465590cd27aed04d2675498fbe59474728
This commit is contained in:
@@ -20,25 +20,27 @@ import com.example.android.apis.R;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Fragment;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class FragmentLayout extends Activity {
|
||||
View mFirstFragmentView;
|
||||
View mSecondFragmentView;
|
||||
|
||||
Fragment mFirstFragment;
|
||||
Fragment mSecondFragment;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.fragment_layout);
|
||||
mFirstFragmentView = findViewById(R.id.fragment1);
|
||||
mSecondFragmentView = findViewById(R.id.fragment2);
|
||||
mFirstFragment = findFragmentById(R.id.fragment1);
|
||||
mSecondFragment = findFragmentById(R.id.fragment2);
|
||||
}
|
||||
|
||||
static class FirstFragment extends Fragment {
|
||||
|
||||
@@ -30,14 +30,17 @@ import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class FragmentStack extends Activity {
|
||||
int mStackLevel;
|
||||
Fragment mLastFragment;
|
||||
int mStackLevel = 1;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.fragment_stack);
|
||||
addFragmentToStack();
|
||||
|
||||
// Add initial fragment.
|
||||
Fragment newFragment = new CountingFragment(mStackLevel);
|
||||
FragmentTransaction ft = openFragmentTransaction();
|
||||
ft.add(newFragment, R.id.simple_fragment).commit();
|
||||
|
||||
// Watch for button clicks.
|
||||
Button button = (Button)findViewById(R.id.next);
|
||||
@@ -52,12 +55,9 @@ public class FragmentStack extends Activity {
|
||||
mStackLevel++;
|
||||
Fragment newFragment = new CountingFragment(mStackLevel);
|
||||
FragmentTransaction ft = openFragmentTransaction();
|
||||
if (mLastFragment != null) {
|
||||
ft.remove(mLastFragment);
|
||||
}
|
||||
ft.add(newFragment, R.id.simple_fragment);
|
||||
mLastFragment = newFragment;
|
||||
ft.addToBackStack();
|
||||
ft.replace(newFragment, R.id.simple_fragment);
|
||||
ft.setTransition(FragmentTransaction.TRANSIT_ACTIVITY_OPEN);
|
||||
ft.addToBackStack(null);
|
||||
ft.commit();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user