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.Activity;
|
||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
import android.app.FragmentTransaction;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.view.View.OnClickListener;
|
|
||||||
import android.widget.Button;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
public class FragmentLayout extends Activity {
|
public class FragmentLayout extends Activity {
|
||||||
View mFirstFragmentView;
|
View mFirstFragmentView;
|
||||||
View mSecondFragmentView;
|
View mSecondFragmentView;
|
||||||
|
|
||||||
|
Fragment mFirstFragment;
|
||||||
|
Fragment mSecondFragment;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.fragment_layout);
|
setContentView(R.layout.fragment_layout);
|
||||||
mFirstFragmentView = findViewById(R.id.fragment1);
|
mFirstFragmentView = findViewById(R.id.fragment1);
|
||||||
mSecondFragmentView = findViewById(R.id.fragment2);
|
mSecondFragmentView = findViewById(R.id.fragment2);
|
||||||
|
mFirstFragment = findFragmentById(R.id.fragment1);
|
||||||
|
mSecondFragment = findFragmentById(R.id.fragment2);
|
||||||
}
|
}
|
||||||
|
|
||||||
static class FirstFragment extends Fragment {
|
static class FirstFragment extends Fragment {
|
||||||
|
|||||||
@@ -30,14 +30,17 @@ import android.widget.Button;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
public class FragmentStack extends Activity {
|
public class FragmentStack extends Activity {
|
||||||
int mStackLevel;
|
int mStackLevel = 1;
|
||||||
Fragment mLastFragment;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.fragment_stack);
|
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.
|
// Watch for button clicks.
|
||||||
Button button = (Button)findViewById(R.id.next);
|
Button button = (Button)findViewById(R.id.next);
|
||||||
@@ -52,12 +55,9 @@ public class FragmentStack extends Activity {
|
|||||||
mStackLevel++;
|
mStackLevel++;
|
||||||
Fragment newFragment = new CountingFragment(mStackLevel);
|
Fragment newFragment = new CountingFragment(mStackLevel);
|
||||||
FragmentTransaction ft = openFragmentTransaction();
|
FragmentTransaction ft = openFragmentTransaction();
|
||||||
if (mLastFragment != null) {
|
ft.replace(newFragment, R.id.simple_fragment);
|
||||||
ft.remove(mLastFragment);
|
ft.setTransition(FragmentTransaction.TRANSIT_ACTIVITY_OPEN);
|
||||||
}
|
ft.addToBackStack(null);
|
||||||
ft.add(newFragment, R.id.simple_fragment);
|
|
||||||
mLastFragment = newFragment;
|
|
||||||
ft.addToBackStack();
|
|
||||||
ft.commit();
|
ft.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user