sharedpreferences between activities(活动之间的共同偏好)
问题描述
这个问题有点类似于其他共享偏好问题,但我真的不知道如何在不同的活动之间准确地使用它?如果可能,请提供完整的活动文件,而不是几行代码,因为我是这个领域的菜鸟!
This question is kind of similar to other sharedpreferences questions but i don't really know how exactly to use it between different activities? Kindly provide the complete activity file if possible rather than just few lines of codes as i am noob in this field!
我有两个活动.一个是userprofile,另一个是edituserprofile.无论用户在 edituserprofile 中编辑什么,只要用户单击 edituserprofile 应用栏中的保存图像按钮,就应该在 userprofile 活动中显示.sharedpreferences 在edituserprofile 中完美运行,用户可以看到输入的数据,也可以更改它,因为它是edittextview.但是,我无法将相同的逻辑应用于用户配置文件活动.当我从edituserprofile单击保存按钮时,它会将我带到用户配置文件,我可以看到在edituserprofile中所做的更改,但是一旦我退出用户配置文件并重新启动它,用户配置文件中的数据就会被清除,而不是来自edituserprofile!我希望 userprofile 保存,显示来自 edituserprofile 的数据,甚至用户退出并重新启动应用程序!
I have two activities. one is userprofile and another is edituserprofile. Whatever a user edits in edituserprofile, should be displayed in userprofile activity as soon as user click on save image button from the app bar of edituserprofile. sharedpreferences works perfectly in edituserprofile where user can see entered data and also able to change it as it is edittextview. However, i am not able to apply the same logic to userprofile activity. When i click on save button from edituserprofile, it takes me to userprofile and i can see the change that has made in edituserprofile but as soon as i exit the userprofile and relaunch it, data gets cleared in userprofile but not from edituserprofile! i want userprofile to save, display data from edituserprofile even user exit and re-launch the app!
以下是用户个人资料活动!
Below is userprofile activity!
package com.example.android.coffeeshop6menus;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class UserProfile extends AppCompatActivity {
public static final int Edit_Profile = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
Toolbar userProfileToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(userProfileToolbar);
SharedPreferences sharedpreferences = getPreferences(MODE_PRIVATE);
displayMessage(sharedpreferences.getString("nameKey", ""));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.userprofile_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_editProfile:
Intent userProfileIntent = new Intent(UserProfile.this, EditUserProfile.class);
startActivityForResult(userProfileIntent, Edit_Profile);
}
return true;
}
// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case Edit_Profile:
if (resultCode == RESULT_OK) {
String name = data.getStringExtra("");
displayMessage(name);
}
break;
}
}
public void displayMessage(String message) {
TextView usernameTextView = (TextView) findViewById(R.id.importProfile);
usernameTextView.setText(message);
}
}
下面是运行完美的edituserprofile活动!
Below is edituserprofile activity that works perfect!
package com.example.android.coffeeshop6menus;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class EditUserProfile extends AppCompatActivity {
private CoordinatorLayout coordinatorLayout;
public static final String Name = "nameKey";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_user_profile);
Toolbar userProfileToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(userProfileToolbar);
TextView usernameTextView = (TextView) findViewById(R.id.username);
sharedpreferences = getSharedPreferences(Name, Context.MODE_PRIVATE);
if (sharedpreferences.contains(Name)) {
usernameTextView.setText(sharedpreferences.getString(Name, ""));
}
coordinatorLayout = (CoordinatorLayout) findViewById(R.id
.coordinatorLayout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.editprofile_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save:
TextView usernameTextView = (TextView) findViewById(R.id.username);
String usernameString = usernameTextView.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, usernameString);
editor.apply();
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Saved!", Snackbar.LENGTH_LONG);
snackbar.show();
Intent userProfileIntent = new Intent(EditUserProfile.this, UserProfile.class);
userProfileIntent.putExtra("", usernameString);
setResult(RESULT_OK, userProfileIntent);
finish();
}
return true;
}
}
下面是userprofile.xml文件
Below is the userprofile.xml file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.coffeeshop6menus.UserProfile">
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Name"
android:textSize="20dp" />
<TextView
android:id="@+id/importProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/userNameImport"
android:textSize="20dp" />
</LinearLayout>
</ScrollView>
下面是edituserprofile xml文件:
Below is the edituserprofile xml file:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.coffeeshop6menus.UserProfile">
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="User Profile"
android:textSize="20dp" />
</LinearLayout>
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:cursorVisible="true"
android:hint="@string/EditTextHint"
android:inputType="textNoSuggestions" />
<EditText
android:id="@+id/usercontact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:cursorVisible="true"
android:hint="@string/usercontactHint"
android:inputType="textNoSuggestions" />
<EditText
android:id="@+id/useremail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:cursorVisible="true"
android:hint="@string/useremailHint"
android:inputType="textEmailAddress" />
</LinearLayout>
</ScrollView>
请帮忙!
推荐答案
在您的 UserProfile
类中,其他任何地方都发生变化 -
In your UserProfile
class and everywhere else change -
SharedPreferences sharedpreferences = getPreferences(MODE_PRIVATE);
SharedPreferences sharedpreferences = getPreferences(MODE_PRIVATE);
通过这个-
sharedpreferences = getSharedPreferences("nameKey", Context.MODE_PRIVATE);
你很高兴!
这篇关于活动之间的共同偏好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:活动之间的共同偏好
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01