java - 将字符串从活动传递到片段
问题描述
我试图将一个字符串从一个活动传递给一个片段,以便稍后将其用作片段中其他事物的键,但它返回为空。我究竟做错了什么?这是我的片段所在的活动代码
import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class UserPage extends AppCompatActivity {
String username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_page);
//this bit works fine, but later I also want to take string username and pass it to fragment
username = getIntent().getExtras().getString("username");
TextView welcome = findViewById(R.id.welcome_text);
welcome.setText("Welcome, " + username);
}
public void flipTab(View v) {
Fragment fragment = null;
switch(v.getId()) {
case R.id.btn_profile:
fragment = new MyProfileFragment();
break;
case R.id.btn_edit:
//another fragment here later
break;
case R.id.btn_colleges:
fragment = new CollegeFragment();
break;
}
//here I want to take the String username that came from the other activity and pass it to fragment
Bundle args = new Bundle();
args.putString("username", username);
fragment.setArguments(args);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fr, fragment);
ft.commit();
}
}
这是我遇到问题的片段
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MyProfileFragment extends Fragment {
private String text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_my_profile, container, false);
TextView example = myFragmentView.findViewById(R.id.ex);
if(getArguments() != null) {
text = getArguments().getString("username");
} else {
example.setText(text);
System.out.println("String=" + text);
}
Button signOut = myFragmentView.findViewById(R.id.user_sign_out);
signOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Logged out");
logOut();
}
});
return myFragmentView;
}
public void logOut() {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
}
}
如果我忽略了一些非常明显的东西,我对片段很陌生。但是我尝试了一些东西,无论如何它们似乎都返回 null 。任何帮助,将不胜感激
解决方案
通过使用 Bundle 或在片段调用方法中传递参数有两种方法
例子
按捆绑
活动中
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
和在片段任务中
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
return inflater.inflate(R.layout.fragment, container, false);
}
推荐阅读
- php - Laravel 5.5 - 从实际模型在特定月份没有数据的模型中获取数据
- html - 带有粘性标题的高级 HTML CSS flexbox 表
- asp.net-mvc-5 - BeginCollectionItem 不绑定
- python - numpy 'isin' 性能改进
- listview - Xamarin Forms - 里面装有 ListView 的卡片的高度非常大
- netsuite - 如何从 SuiteScript 2.0 Suitelet 中检索值以进行 POST?
- c# - LINQ2SQL 表达式 - 转换日期时转换失败
- c# - DependencyObject 不能很好地处理对象属性的相等性
- fortran - Fortran 不对数组或程序变量调用 FINAL
- php - 如何将按钮文本从阅读更多更改为帖子类别和 wordpress 中的其他文本?