Android--fragment and activity and jump implementation between two fragments

In the interaction of the application, I may need to implement:

  • Jump from the current fragment to another fragment
  • Jump from the current fragment to an activity
  • Jump from the current activity to a fragment

There are many ideas provided on the Internet, here is a summary of a set of own methods.

1. Jump from the current fragment to another fragment 
1. Declare the fragment management object and transaction object in the corresponding fragment global.

// Fragment管理对象
    private FragmentManager manager;
    private FragmentTransaction ft;
  • 1
  • 2
  • 3

2. Initialize the fragment management object in the OnCreate method

@Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        manager = getFragmentManager();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3. Get a FragmentTransaction instance from FragmentManager

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

So how can you still pass parameters when the fragment is switched?

We can use the setArguments() method to bind a bundle object and pass it to another fragment.

myJDEditFragment.setArguments(bundle);
  • 1

In another fragment (myJDEditFragment) use getArguments() to get the bundle object.

2. Jump from the current fragment to an activity 
Since the fragment can use getActivity() to access the Activity instance, the implementation of this step is very simple.

intent=new Intent(getActivity(), UserLoginActivity.class);
startActivity(intent);
  • 1
  • 2

3. Jump from the current activity to a fragment 
If you are jumping from the fragment to the activity, and then want to jump back to the fragment from the activity, you can simply finish the activity.

如果需求是这样的: 
在“更多”页面里点击“账号管理”弹出一个activity,处理完这个activity后我们要跳转到”我的京东“中,该如何实现呢? 
这里写图片描述 
这里写图片描述 
1、 在对应activity中使用意图跳转到MainActivity中,这里通过意图塞入了一个标识符(更严谨的方式是通过请求码和结果码实现)。

// 登录成功跳转到我的京东首页
Intent intent = new Intent(UserLoginActivity.this,MainActivity.class);
intent.putExtra("userloginflag", 1);
startActivity(intent);
  • 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

附上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
<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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325887612&siteId=291194637