fragment1的XML fragment2的XML 主文件的JAVA代码 package com.example.mpyypm.fragmentzy; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.KeyEvent; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ fragment1 fragment1; fragment2 fragment2; private boolean qiehuan = true; private boolean back = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); fragment1 = new fragment1(); transaction.add(R.id.bnt_show,fragment1); transaction.commit(); } @Override public void onClick(View view) { if(view.getId() == R.id.bnt1){ back = true; if(qiehuan){ FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); if (fragment2 == null){ fragment2 = new fragment2(); transaction.replace(R.id.bnt_show,fragment2); transaction.commit(); qiehuan = false; } else{ transaction.replace(R.id.bnt_show,fragment2); transaction.commit(); qiehuan = false; } } else{ Toast.makeText(this,"This is second fragment",Toast.LENGTH_SHORT).show(); } } } public boolean onKeyDown(int keyCode, KeyEvent event) { if(event.getKeyCode()==KeyEvent.KEYCODE_BACK&&back){ FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); qiehuan = true; back = false; transaction.replace(R.id.bnt_show,fragment1); transaction.commit(); return false; } else { finish(); } return super.onKeyDown(keyCode, event); } } fragment1的JAVA代码 package com.example.mpyypm.fragmentzy; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class fragment1 extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_fragment1, container, false); return view; } } fragment2的JAVA代码 package com.example.mpyypm.fragmentzy; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class fragment2 extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.fragment_fragment2,container,false); return view; } }
package com.example.mpyypm.fragmentzy; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.KeyEvent; import android.view.View; import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ fragment1 fragment1; fragment2 fragment2; private boolean qiehuan = true; private boolean back = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); fragment1 = new fragment1(); transaction.add(R.id.bnt_show,fragment1); transaction.commit(); } @Override public void onClick(View view) { if(view.getId() == R.id.bnt1){ back = true; if(qiehuan){ FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); if (fragment2 == null){ fragment2 = new fragment2(); transaction.replace(R.id.bnt_show,fragment2); transaction.commit(); qiehuan = false; } else{ transaction.replace(R.id.bnt_show,fragment2); transaction.commit(); qiehuan = false; } } else{ Toast.makeText(this,"This is second fragment",Toast.LENGTH_SHORT).show(); }
} } public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getKeyCode()==KeyEvent.KEYCODE_BACK&&back){
FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); qiehuan = true; back = false; transaction.replace(R.id.bnt_show,fragment1); transaction.commit(); return false; } else { finish(); } return super.onKeyDown(keyCode, event); }
}
package com.example.mpyypm.fragmentzy; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
public class fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_fragment1, container, false); return view; } }
public class fragment2 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.fragment_fragment2,container,false); return view; } }