更改活动时保存数据

时间:2018-09-19 06:49:05

标签: android login kotlin

更改活动时,我在保存数据时遇到问题。

我使用EditText进行了初始登录,人们可以在其中编写用于登录的参数。

附加xml代码:

 <EditText
    android:id="@+id/portText"
    android:layout_width="300dp"
    android:layout_height="30dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="4dp"
    android:background="@drawable/shape"
    android:hint="  Port Tcp"
    android:lineSpacingExtra="8sp"
    android:singleLine="true"
    android:textAlignment="textStart"
    android:textColorLink="@android:color/background_dark"
    android:textSize="10sp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/idText" />

当用户按下登录按钮时,活动更改。 当用户返回登录页面时,如何保存并向用户显示初始数据?

4 个答案:

答案 0 :(得分:0)

就像@Teptis和@Nero所说的那样,您可以在登录时使用共享首选项来保存数据。 使用共享首选项存储的数据即使在应用程序完全关闭后也可用。这是在共享首选项中存储数据的方式

     val pref: SharedPreferences = getSharedPreferences("RandomNameForKey",Context.MODE_PRIVATE)
                    val editor: SharedPreferences.Editor = pref.edit()//You need editor for writing something to shared preferences
                    editor.putBoolean("user_logged_In", true)//You can put Boolean ,String ,etc whatever you want (boolean in my case)
                    editor.putString("login_access_token",response.body()?.data?.access_token)//String example
                    editor.commit()//Commit to make changes

一旦存储,现在就可以这样访问

     val pref: SharedPreferences = getSharedPreferences("sameKeyAsAbove", Context.MODE_PRIVATE)//Same Key name used while storing values(Important)
    accessToken = pref.getString("login_access_token", "DEFAULT")//Same key name as above while storing string(Access token is a string variable)

另一件事,从共享的首选项访问数据时,您将不需要编辑器,而在存储数据时,您将需要它

只是不要弄乱钥匙,否则一切都会出错

HappyCoding

答案 1 :(得分:0)

SharedPreferences是您要寻找的。看起来像这样。

public class WidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate (Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        //Update your widgets here
    }
}

答案 2 :(得分:0)

//单击按钮时的第一个活动。

SharedPreferences prefs = getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = Constant.prefs.edit();
editor.putString("loginvalue", portText.getText.toString());
editor.commit();

//第二活动

SharedPreferences prefs = getSharedPreferences("MyPref", MODE_PRIVATE);
if(prefs.contains("loginvalue")){youtextview.setText(prefs 
.getString("loginvalue",""));}

尝试一下,看看是否有帮助?

答案 3 :(得分:0)

在您的用例中使用SharedPreferences。以下是实现所需内容的示例代码。

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val sharedPref = this.getSharedPreferences("myPrefs", Context.MODE_PRIVATE)

if (!sharedPref.getString("login", "").isEmpty()) {
    portText.setText(sharedPref.getString("login", ""))
}


loginButton.setOnClickListener {
    val login = portText.text.toString()
    sharedPref.edit().putString("login", login).apply()
    startActivity(Intent(this, SecondActivity::class.java))
}