Created
July 24, 2017 13:15
-
-
Save YoKeyword/7b9292455198d70ecd3fd1522dbfd78f to your computer and use it in GitHub Desktop.
FragmentOverlapTest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AFragment extends Fragment { | |
public static AFragment newInstance() { | |
AFragment fragment = new AFragment(); | |
return fragment; | |
} | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_a, container, false); | |
view.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
((MainActivity)getActivity()).startToB(); | |
} | |
}); | |
return view; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BFragment extends Fragment { | |
public static BFragment newInstance() { | |
BFragment fragment = new BFragment(); | |
return fragment; | |
} | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_b, container, false); | |
return view; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private AFragment mAFragment; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mAFragment = (AFragment) getSupportFragmentManager().findFragmentByTag("A"); | |
if (mAFragment == null) { | |
mAFragment = AFragment.newInstance(); | |
getSupportFragmentManager().beginTransaction() | |
.add(R.id.fl, mAFragment, "A") | |
.addToBackStack("A") | |
.commit(); | |
} | |
} | |
public void startToB() { | |
getSupportFragmentManager().beginTransaction() | |
.add(R.id.fl, BFragment.newInstance(), "B") | |
.hide(mAFragment) | |
.addToBackStack("B") | |
.commit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment