2017-08-11 102 views
1

我正在开发应用程序,我试图(和失败)在活动链和碎片之间传递数据。现在,我的登录/注册活动可以完美工作,并将用户的凭证保存在在线的MySql数据库中,并且当用户登录时,某些凭证将传递给主活动,其中包含显示碎片的容器。Android:从随机碎片/活动访问用户登录数据

我没有遇到任何困难,从这个主要活动传递数据到其中一个片段使用一个包,但是,我现在有几个从页面连接的片段按顺序访问:MainScreen - ProfileFragment - EditProfileFragment - ChangeUserName(一个弹出式活动)。 EditProfile frag也有一个changeUserPicture按钮,但它将使用与名称更改相同的方法。

我需要将用户的凭据从MainScreen传递给ChangeUserName活动。我已经尝试过使用捆绑链来创建活动/碎片时传递字符串,但无法正常工作。

我想使用一个接口来传递这些数据,但我不能解决如何正确地做到这一点,任何帮助将不胜感激!

对于相关的活动和片段的代码如下:

MainScreen:

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(只包含目前两个按钮):

public class EditProfileFragment extends Fragment { 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_edit_profile, container, false); 

    Button editName = (Button) view.findViewById(R.id.changeUsernameButton); 
    Button editPicture = (Button) view.findViewById(R.id.changeProfilePictureButton); 


    editName.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 


      startActivity(new Intent(getActivity(), PopupNameChange.class)); 
     } 
    }); 

    return view; 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    TextView title = (TextView) getActivity().findViewById(R.id.titleText); 
    title.setText("Edit 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)); 

} 

}

PopupNameChange(打开更改用户名的活动。它会弹出在屏幕中央的片段以上,并且不覆盖以前的观点):

public class PopupNameChange extends Activity implements PracticeInterface{ 

String userName; 

public void onFragmentInteraction(String userDataString) { 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.pop_up_change_name); 

    TextView userNameCurrent = (TextView) findViewById(R.id.tvCurrentName); 

    //I want to pass the user's name to this activity to change the string of userNameCurrent 
    userNameCurrent.setText(userName); 

    DisplayMetrics dm = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 


    int width = dm.widthPixels; 
    int height = dm.heightPixels; 

    getWindow().setLayout(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT); 

    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

} 

}

回答

1

所以,才达到你的目标最快的方式将是使用SharedPreferences。链接到谷歌site。基本上,当你在主要活动数据,instert该数据SharedPreferences这样的:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedPref.edit(); 
editor.putString("userName", userName); 
editor.commit(); 

而在你ChangeUserName获取当前用户名:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 
String userName = sharedPref.getString("userName", defaultValue); 

我shold也警告你。请勿将敏感数据保存在SharedPreferences中。使用固定电话的用户很容易从SharedPreferences获取数据。

+0

我得到一个nullPointerException与此,并与其他一些sharedPreference的东西,如getDefaultSharedPreferences。主活动中的数据插入位于onCreate方法中,而Name Change中的数据读取代码不在onCreate中,这是否会产生影响? – JSteward

+0

没关系,我添加了getApplicationContext()而不是getActivity(),它允许我将它放在Name Change活动的onCreate中。谢谢你的帮助!! – JSteward