只是一個小問題,也許很難,也許不是。 但就像標題所說,按下按鈕後是否可以啓動新的fragmentActivity?按下按鈕後,是否可以啓動一個片段作爲新的片段活動?
現在我一直在研究這個問題,我寧願不再開始一個新的活動,除非我必須要講課。 到目前爲止,我對我的結果感到滿意,因爲我嘗試在一個類中爲三個不同的按鈕創建三個不同的片段包。
因此,簡而言之,有3個按鈕,他們需要激活自己的片段包。
這裏是我當前的代碼我想嘗試修復:
MainActivity:
public class MainActivity extends Activity {
//ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* viewpager = (ViewPager)findViewById(R.id.pager);
PagerAdapter padater = new PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(padater);*/
}
public boolean contentSwitch = false;
@Override
public void onBackPressed() {
if(contentSwitch) {
finish();
Log.i("BtnHandleEvent", "BackButton Pressed!");
Intent startIntent = new Intent(MainActivity.this, MainActivity.class);
startActivity(startIntent);
contentSwitch = false;
} else {
Log.i("Event Handler", "Terminating app!");
Runtime.getRuntime().runFinalization(); //No idea why I did this
Runtime.getRuntime().gc(); //No idea why I did this too
Runtime.getRuntime().exit(1); //Doesn't cleanup?
}
}
//start fragment activity
public void btn1(View v) {
contentSwitch = true;
}
public void btn2(View v) {
contentSwitch = true;
}
public void btn3(View v) {
contentSwitch = true;
}}
FragmentPackage:
public class FragmentPackage extends FragmentPagerAdapter {
public static boolean ViewPackOne = false; //Still to do
//Still need to fix
public FragmentPackage(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int arg0) {
switch(arg0) {
case 0:
if(ViewPackOne) {
return new FragmentOne();
}
case 1:
if(ViewPackOne){
return new FragmentTwo();
}
case 2:
if(ViewPackOne) {
return new FragmentThree();
}
default:
break;
}
return null;
}
public int getCount() {
return 3;
}
public static class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
return inflater.inflate(R.layout.fragment_one_layout, container, false);
}
}
public static class FragmentTwo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
return inflater.inflate(R.layout.fragment_two_layout, container, false);
}
}
public static class FragmentThree extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
return inflater.inflate(R.layout.fragment_one_layout, container, false);
}
}
}
啓動它是的,你可以做到這一點 –
究竟如何?目前我完全沒有想法。 – Davey