Activity通讯Fragment

package com.example.repastapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

 private RadioButton f3;

 private long time;

//需要跳转的Fragment
 private Fragment3 fra3;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  f3 = (RadioButton) this.findViewById(R.id.bt3);

//为了管理Activity中的Fragment所以需要FragmentManager 为了得到它需要getSupportFragmentManager()方法

  FragmentManager fm = this.getSupportFragmentManager();
  FragmentTransaction ft = fm.beginTransaction();
 }

 public void onGoF3(View v) {
  f3.setChecked(false);
  FragmentManager fm = this.getSupportFragmentManager();
  FragmentTransaction ft = fm.beginTransaction();
  fra3 = new Fragment3();

  ft.replace(R.id.fl, fra3);
  ft.commit();
 }

//返回退出处理
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {

  if (keyCode == KeyEvent.KEYCODE_BACK) {
   if ((event.getEventTime() - time) > 3000) {
    Toast.makeText(MainActivity.this, "再按返回,退出应用",
      Toast.LENGTH_SHORT).show();
    time = event.getEventTime();

   } else {
    android.os.Process.killProcess(android.os.Process.myPid());
   }
  }
  return true;
 }

}

你可能感兴趣的:(Activity通讯Fragment)