Fragment跳转并传参给activity

实现Fragment跳转activity页面,并传参数过去,也就是使用Intent和Bundle配合使用来实现的。附上源码:

Fragment

public class HomeFragment extends Fragment {
     
@Nullable
    @Override
   
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

         //Fragment布局view对象
View view = inflater.inflate(R.layout.fragment_home, container, false);
//初始化页面
        transfer ();
       
return view

}

public void transfer () {
 
//跳转页面
Intent intent=new Intent(mActivityContext,SearchResultActivity.class);
//传递参数
Bundle bundle=new Bundle();
bundle.putInt(
"RTableCategoryID",RTableCategoryID);
intent.putExtras(bundle);
startActivity(intent);

}

Activity

public class SearchResultActivity  extends AppCompatActivity {
    
@Override
   
protected void onCreate(@Nullable Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
              
//设置布局
        setContentView(R.layout.activity_search_result);
              
//获取上一个页面传递过来的参数
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
       
int RTableCategoryID=bundle.getInt("RTableCategoryID");
 }

 

你可能感兴趣的:(Android)