fragment与activity及两个fragment之间的跳转实现
在近期的练手项目中,我们使用到了Android Fragment。在应用的交互中,我可能需要实现:
- 从当前的fragment跳转到另一个fragment
 - 从当前的fragment跳转到一个activity中
 - 从当前的activity跳转到一个fragment中
 
网上提供的思路较多,这里总结了一套自己的方法。
一、从当前的fragment跳转到另一个fragment 
1.在相应fragment全局中声明fragment管理对象及事务对象。
// Fragment管理对象
    private FragmentManager manager;
    private FragmentTransaction ft;
- 1
 - 2
 - 3
 
- 1
 - 2
 - 3
 
2.在OnCreate方法中初始化fragment管理对象
@Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        manager = getFragmentManager();
    }
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 
3.从 FragmentManager 获得一个FragmentTransaction实例
MyJDEditFragment myJDEditFragment = new MyJDEditFragment();
ft = manager.beginTransaction();
//当前的fragment会被myJDEditFragment替换
ft.replace(R.id.realtabcontent, myJDEditFragment);
ft.addToBackStack(null);
ft.commit();
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 
那么如何在fragment切换的时候还可以传递参数呢?
我们可以使用setArguments()方法绑定一个bundle对象传递到另外一个fragment中。
myJDEditFragment.setArguments(bundle);
- 1
 
- 1
 
在另外一个fragment中(myJDEditFragment)使用getArguments()可以拿到bundle对象。
二、从当前的fragment跳转到一个activity中 
由于fragment可以使用 getActivity() 访问Activity实例,所以这一步实现很简单。
intent=new Intent(getActivity(), UserLoginActivity.class);
startActivity(intent);
- 1
 - 2
 
- 1
 - 2
 
三、从当前的activity跳转到一个fragment中 
如果是从fragment跳转到activity中,然后想从这个activity中再跳转回fragment,这时直接finish这个activity即可。
如果需求是这样的: 
在“更多”页面里点击“账号管理”弹出一个activity,处理完这个activity后我们要跳转到”我的京东“中,该如何实现呢?  
 
1、 在对应activity中使用意图跳转到MainActivity中,这里通过意图塞入了一个标识符(更严谨的方式是通过请求码和结果码实现)。
// 登录成功跳转到我的京东首页
Intent intent = new Intent(UserLoginActivity.this,MainActivity.class);
intent.putExtra("userloginflag", 1);
startActivity(intent);
- 1
 - 2
 - 3
 - 4
 
- 1
 - 2
 - 3
 - 4
 
2.在MainActivity的onResume()方法中得到这个标识符,并且切换到相应的Tab即可。
@Override
    protected void onResume() {
        int id = getIntent().getIntExtra("userloginflag", 0);
        if (id == 1 ) {
            mTabHost.setCurrentTab(3);
            //3代表”我的京东“所在条目的位置,参考下面的源码即可理解
        }
        super.onResume();
    }
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 
附上MainActiviy.Java的源码及布局文件activity_main.xml
package com.example.jds;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import com.example.jds.fragment.CarFragment;
import com.example.jds.fragment.CategoryFragment;
import com.example.jds.fragment.IndexFragment;
import com.example.jds.fragment.MoreFragment;
import com.example.jds.fragment.MyJDIndexFragment;
import com.example.jds.fragment.SettingFragment;
import com.example.jds.util.UserApplication;
public class MainActivity extends FragmentActivity {
    /**
     * -----------------------------------------------
     */
    public MyJDIndexFragment myJDIndexFragment;
    // 更多页面
    public MoreFragment moreFragment;
    // 设置页面
    public SettingFragment settingFragment;
    // 声明控件对象
    public FragmentTabHost mTabHost;
    // 布局填充器
    private LayoutInflater inflater;
    // 存放文本的数组
    private int tabHostTextArray[] = { R.string.tabhost_index,
            R.string.tabhost_category, R.string.tabhost_car,
            R.string.tabhost_my_jd, R.string.tabhost_more };
    // 存放图标的数组
    private int tabHostIconArray[] = { R.drawable.tab_home_index,
            R.drawable.tab_home_category, R.drawable.tab_home_car,
            R.drawable.tab_home_my_jd, R.drawable.tab_home_more };
    // 声明片段对应的数组
    private Class fragments[] = { IndexFragment.class, CategoryFragment.class,
            CarFragment.class, MyJDIndexFragment.class, MoreFragment.class };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化布局填充器对象
        inflater = LayoutInflater.from(this);
        // 查找tabHost对象
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        // 启动tabHost
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        // 遍历片段
        for (int i = 0; i < fragments.length; i++) {
            // 创建一个TabSpec
            TabSpec tabSpec = mTabHost.newTabSpec(
                    getResources().getString(tabHostTextArray[i]))
                    .setIndicator(getTabItemView(i));
            // 加入到TabHost中
            mTabHost.addTab(tabSpec, fragments[i], null);
        }
        initview();
    }
    public View getTabItemView(int i) {
        View view = inflater.inflate(R.layout.tab_nav_item, null);
        ImageView tab_nav_img = (ImageView) view.findViewById(R.id.tab_nav_img);
        TextView tab_nav_text = (TextView) view.findViewById(R.id.tab_nav_text);
        // 设置图片及文本
        tab_nav_img.setImageResource(tabHostIconArray[i]);
        // Toast.makeText(this, tabHostIconArray[i], 1).show();
        tab_nav_text.setText(getResources().getString(tabHostTextArray[i]));
        return view;
    }
    /**
     * fragment
     */
    private void initview() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        moreFragment = new MoreFragment();
        myJDIndexFragment = new MyJDIndexFragment();
        // ft.add(R.id.realtabcontent, accountManagerFragment);
        // ft.add(R.id.realtabcontent, myJDFragment);
        settingFragment = new SettingFragment();
        /*
         * ft.add(R.id.realtabcontent, accountManagerFragment);
         * ft.add(R.id.realtabcontent, myJDFragment);
         * ft.add(R.id.realtabcontent,myJDIndexFragment);
         * ft.add(R.id.realtabcontent,)
         */
        ft.commit();
    }
    @Override
    protected void onResume() {
        int id = getIntent().getIntExtra("userloginflag", 0);
        if (id == 1 ) {
            mTabHost.setCurrentTab(3);
        }
        super.onResume();
    }
}
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 - 71
 - 72
 - 73
 - 74
 - 75
 - 76
 - 77
 - 78
 - 79
 - 80
 - 81
 - 82
 - 83
 - 84
 - 85
 - 86
 - 87
 - 88
 - 89
 - 90
 - 91
 - 92
 - 93
 - 94
 - 95
 - 96
 - 97
 - 98
 - 99
 - 100
 - 101
 - 102
 - 103
 - 104
 - 105
 - 106
 - 107
 - 108
 - 109
 - 110
 - 111
 - 112
 - 113
 - 114
 - 115
 - 116
 - 117
 - 118
 - 119
 - 120
 
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 - 71
 - 72
 - 73
 - 74
 - 75
 - 76
 - 77
 - 78
 - 79
 - 80
 - 81
 - 82
 - 83
 - 84
 - 85
 - 86
 - 87
 - 88
 - 89
 - 90
 - 91
 - 92
 - 93
 - 94
 - 95
 - 96
 - 97
 - 98
 - 99
 - 100
 - 101
 - 102
 - 103
 - 104
 - 105
 - 106
 - 107
 - 108
 - 109
 - 110
 - 111
 - 112
 - 113
 - 114
 - 115
 - 116
 - 117
 - 118
 - 119
 - 120
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     android:background="#DDDDDD"
    tools:context="${relativePackage}.${activityClass}" >
    <FrameLayout 
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_tabhost">
        <FrameLayout 
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>
                    
                
                
            
        
浙公网安备 33010602011771号