当我去另一个活动时,我的问题是在主要活动中保存数据。在练习程序中,我在主要活动中有2个按钮,当您单击第一个按钮时,您将进入第二个名为firstapp的活动,只有一个评级栏。我选择你的评级,然后按下后退按钮,在按钮下面的textview中,它获得评级栏的字符串值并告知评级。但是,当我单击第二个按钮并移动到名为secondapp的第3个活动并进行评级时,然后返回主活动我在第二个文本视图中看到正确的信息,但我做的第一个评级现在已从文本视图中消失。
我不知道如何在那里保存这些信息,并且现在已经搞乱了一段时间
package com.example.brent.appmanagement;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView firstrating, secondrating;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingapp1();
ratingapp2();
getrating();
}
private void getrating(){
//firstrating = findViewById(R.id.savedrating1);
// secondrating = findViewById(R.id.savedrating2);
//Intent i = getIntent();
//firstrating.setText(i.getExtras().getString("rating"));
// secondrating.setText(i.getStringExtra("rating2"));
Bundle extras = getIntent().getExtras();
if (extras != null) {
String rating = extras.getString("rating");
firstrating = findViewById(R.id.savedrating1);
firstrating.setText(rating);
String rating2 = extras.getString("rating2");
secondrating = findViewById(R.id.savedrating2);
secondrating.setText(rating2);
}
}
private void ratingapp1(){
Button rateapp1 = findViewById(R.id.rateapp1);
rateapp1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT){
Toast.makeText(getApplicationContext(),
"Sorry you need to upgrade your phone before you
can rate the apps", Toast.LENGTH_LONG).show();
}else {
Intent intent = new Intent(getApplicationContext(),
firstapp.class);
startActivity(intent);
}
}
});
}
private void ratingapp2(){
Button rateapp2 = findViewById(R.id.rateapp2);
rateapp2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT){
Toast.makeText(getApplicationContext(),
"Sorry you need to upgrade your phone before you
can rate the apps", Toast.LENGTH_LONG).show();
}else {
// Bundle extras = getIntent().getExtras();
// if(extras != null) {
// firstrating = findViewById(R.id.savedrating1);
// firstrating.setText(extras.getString("rating"));
// }
Intent intent2 = new Intent(getApplicationContext(),
secondapp.class);
startActivity(intent2);
}
}
});
}
}
firstapp活动代码
package com.example.brent.appmanagement;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;
public class firstapp extends AppCompatActivity {
public RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firstapp);
ratingBar = findViewById(R.id.ratingBar);
return1();
}
public void rateMe(View view){
Toast.makeText(getApplicationContext(),
String.valueOf(ratingBar.getRating()),
Toast.LENGTH_LONG).show();
}
private void return1(){
Button returntomain1 = findViewById(R.id.returntomain1);
returntomain1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
i.putExtra("rating", "You have rated this app " +
String.valueOf(ratingBar.getRating() + " stars out of 5"));
startActivity(i);
}
});
}
}
secondapp活动
package com.example.brent.appmanagement;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;
public class secondapp extends AppCompatActivity {
public RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondapp);
return2();
ratingBar = findViewById(R.id.ratingBar);
}
public void rateMe(View view){
Toast.makeText(getApplicationContext(),
String.valueOf(ratingBar.getRating()),
Toast.LENGTH_LONG).show();
}
public void return2(){
Button returntomain2 = findViewById(R.id.returntomain2);
returntomain2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
i.putExtra("rating2", "you have rated this app " +
String.valueOf(ratingBar.getRating() + " stars out of 5"));
startActivity(i);
}
});
}
}
不要介意注释掉的代码,如果你需要我的xml就这么说,我只是不想发帖到很多看,提交按钮和吐司消息就是这样我才能看到它正在向右拉值。如果我正在做的事情我不应该请也请告诉我,因为我说我是新手知识这只是练习
感谢所有
好吧所以我尝试了startActivityForResult并设置了结果并覆盖了onActivityresult方法,即使在我回到主要活动后刚刚调用finish()而不是startActivity时我仍然有相同的结果...同样的事情发生了我去进入firstapp速率它回到主要,我的数据已转移到主要,现在在测试框....但然后我转到第二个应用程序率它回来了,来自文本框中的第一个应用程序的数据现在去
答案 0 :(得分:1)
您的问题是,当您返回时,您正在启动MainActivity的新实例,从而丢失所有以前的数据。你想要做的是调用startActivityForResult并在方法return1和return2()中调用firstapp和secondapp,你应该调用setResult()传递RESULT_OK常量和一个包含你想要MainActivity知道的数据的intent。然后立即致电完成。在MainActivity中,您希望覆盖onActivityResult()方法,该方法将接收您传递给setResult()的数据。
这是开发人员网站的link,用于解释此API。
答案 1 :(得分:0)
第一个我建议您不要创建MainActivity的新实例,只需返回堆栈中当前活动(第一个应用程序或第二个应用程序)下的MainActivity。
关于处理评级回报:
SharedPreferences
来保存第一个/第二个应用活动中的评分。这是link解释SharedPreferences
。startActivityForResult()
开始第一个/第二个应用活动,以便在您的MainActivity中的onActivityResult()
上回电。onActivityResult()
,您可以在此处获得SharePreferences
的数据评级并更新TextView。<强>更新强> 试试这个
private void return1(){
Button returntomain1 = findViewById(R.id.returntomain1);
returntomain1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putFloat("rating1", ratingBar.getRating());
editor.commit();
finish();
}
});
}
和
public void return2(){
Button returntomain2 = findViewById(R.id.returntomain2);
returntomain2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putFloat("rating2", ratingBar.getRating());
editor.commit();
finish();
}
});
}
并在您的MainActivity中
@Override
protected void onResume() {
super.onResume();
updateRating();
}
private void updateRating(){
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
float rating1 = sharedPref.getFloat("rating1",-1);
float rating2 = sharedPref.getFloat("rating2",-1);
// -1 is default value
if(rating1 != -1) {
firstrating.setText(String.valueOf(rating1));
}
if(rating2 != -1) {
secondrating.setText(String.valueOf(rating2));
}
}
希望有所帮助!