Java Android:从随机片段/活动访问用户登录数据
我正在开发一个应用程序,我正在尝试(但失败)在一系列活动和片段之间传递数据。目前,我有一些登录/注册活动,它们工作得很好,可以将用户的凭据保存在在线MySqli数据库上,当用户登录时,一些凭据会传递给主活动,主活动包含一个显示片段的容器 我使用一个bundle将数据从这个主活动传递到它的一个片段没有任何问题,但是,现在我有几个子页面链接到一个概要文件片段,它们是按以下顺序访问的:MainScreen-ProfileFragment-EditProfileFragment-ChangeUserName(一个弹出式活动)。EditProfile框架也有一个changeUserPicture按钮,但它将使用与名称更改相同的方法 我需要将用户的凭据从MainScreen传递到ChangeUserName活动。我曾经尝试过使用一个捆绑链,在创建活动/片段时传递字符串,但没有正常工作 我正在尝试使用一个接口来传递这些数据,但我无法确定如何正确地传递,在此方面的任何帮助都将不胜感激 相关活动和片段的代码如下: 主屏幕:Java Android:从随机片段/活动访问用户登录数据,java,android,android-fragments,Java,Android,Android Fragments,我正在开发一个应用程序,我正在尝试(但失败)在一系列活动和片段之间传递数据。目前,我有一些登录/注册活动,它们工作得很好,可以将用户的凭据保存在在线MySqli数据库上,当用户登录时,一些凭据会传递给主活动,主活动包含一个显示片段的容器 我使用一个bundle将数据从这个主活动传递到它的一个片段没有任何问题,但是,现在我有几个子页面链接到一个概要文件片段,它们是按以下顺序访问的:MainScreen-ProfileFragment-EditProfileFragment-ChangeUserNa
public class MainScreen extends FragmentActivity implements View.OnClickListener {
ImageButton homeButton, searchButton, messageButton, contactsButton;
ImageView Luna;
ConstraintLayout homeView, searchView, lunaView, inboxView, contactsView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
final ImageButton homeButton = (ImageButton) findViewById(R.id.bottom_home);
final ImageButton searchButton = (ImageButton) findViewById(R.id.bottom_search);
final ImageView Luna = (ImageView) findViewById(R.id.bottom_luna);
final ImageButton messageButton = (ImageButton) findViewById(R.id.bottom_messages);
final ImageButton contactsButton = (ImageButton) findViewById(R.id.bottom_contacts);
final ConstraintLayout homeView = (ConstraintLayout) findViewById(R.id.home_section);
final ConstraintLayout searchView = (ConstraintLayout) findViewById(R.id.search_section);
final ConstraintLayout lunaView = (ConstraintLayout) findViewById(R.id.luna_section);
final ConstraintLayout inboxView = (ConstraintLayout) findViewById(R.id.inbox_section);
final ConstraintLayout contactsView = (ConstraintLayout) findViewById(R.id.contacts_section);
final ImageButton profileButton = (ImageButton) findViewById(R.id.profile_button);
final TextView titleBarText = (TextView) findViewById(R.id.titleText);
homeView.setOnClickListener(this);
searchView.setOnClickListener(this);
lunaView.setOnClickListener(this);
inboxView.setOnClickListener(this);
contactsView.setOnClickListener(this);
if (findViewById(R.id.container) != null) {
if (savedInstanceState != null) {
return;
}
HomeFragment firstFragment = new HomeFragment();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.container, firstFragment)
.commit();
titleBarText.setText("Home");
}
/* This is the old toolbar button code. Keep just in case
homeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new HomeFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
*/
Intent intent = getIntent();
final Integer user_id = intent.getIntExtra("user_id", 0);
final String name = intent.getStringExtra("name");
final String email = intent.getStringExtra("email");
profileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment profileFragment = new ProfileFragment();
Bundle profileBundle = new Bundle();
profileBundle.putInt("idKey", user_id);
profileBundle.putString("nameKey", name);
profileBundle.putString("emailKey", email);
profileFragment.setArguments(profileBundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, profileFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
@Override
public void onClick(View view) {
Fragment fragment = new Fragment();
switch (view.getId()){
case R.id.home_section:
fragment = new HomeFragment();
break;
case R.id.search_section:
fragment = new SearchFragment();
break;
case R.id.luna_section:
fragment = new LunaFragment();
break;
case R.id.inbox_section:
fragment = new MessageFragment();
break;
case R.id.contacts_section:
fragment = new ContactsFragment();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
@Override
public void onBackPressed()
{
if(getFragmentManager().getBackStackEntryCount() > 0)
getFragmentManager().popBackStack();
else
super.onBackPressed();
}
}
ProfileFragment:
public class ProfileFragment extends Fragment{
MainScreen activity = (MainScreen) getActivity();
public ProfileFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_profile, container, false);
final TextView userName = (TextView) view.findViewById(R.id.userFullName);
final TextView userWorkplace = (TextView) view.findViewById(R.id.workplace);
final Button workspaceButton = (Button) view.findViewById(R.id.workspaceButton);
final Button socialButton = (Button) view.findViewById(R.id.socialspaceButton);
final Button editProfileButton = (Button) view.findViewById(R.id.editProfileButton);
final Button preferencesButton = (Button) view.findViewById(R.id.preferencesButton);
final Button appSettingsButton = (Button) view.findViewById(R.id.appSettingsButton);
Bundle interfaceBundle = this.getArguments();
if (interfaceBundle != null) {
String nameBundle = getArguments().getString("nameKey"); //Get pass data with its key value
userName.setText(nameBundle);
} else {
userName.setText("No Username Found");
}
editProfileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new EditProfileFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
@Override
public void onResume() {
super.onResume();
TextView title = (TextView) getActivity().findViewById(R.id.titleText);
title.setText("Profile");
ImageButton icon = (ImageButton) getActivity().findViewById(R.id.profile_button);
icon.setColorFilter(getResources().getColor(R.color.coolWhite));
}
@Override
public void onPause() {
super.onPause();
ImageButton icon = (ImageButton) getActivity().findViewById(R.id.profile_button);
icon.setColorFilter(getResources().getColor(R.color.slightGrey));
}
}
EditProfileFragment(目前仅包含两个按钮):
}
PopupNameChange(打开以更改用户名的活动。它会在屏幕中央的片段上方弹出,并且不会覆盖以前的视图):
}因此,实现目标的最快方法是使用
共享参考资料。链接到谷歌。基本上,当您在主活动中获取数据时,将该数据插入SharedReferences
,如下所示:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("userName", userName);
editor.commit();
并在您的中更改用户名
以获取当前用户名:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String userName = sharedPref.getString("userName", defaultValue);
我也应该警告你。不要将敏感数据保存在SharedReferences
中。有根手机的用户很容易从SharedReferences
获取数据,我得到了一个nullPointerException,还有一些其他的SharedReferences,比如GetDefaultSharedReferences。主活动中的数据插入在onCreate方法中,而名称更改中的数据读取代码不在onCreate中,这会有区别吗?不管怎样,我添加了getApplicationContext()而不是getActivity(),它允许我将其放在名称更改活动的onCreate中。谢谢你的帮助!!
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("userName", userName);
editor.commit();
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String userName = sharedPref.getString("userName", defaultValue);