TextView更改值

时间:2018-01-10 19:46:08

标签: java android android-studio

将Spinner选中的值从Blank Activity返回到Second Activity时,它将作为Second Activity更改中的文本。但是当我回到First Activity然后回到Second Activity时,它就失去了它的价值。

FirstActivity.java

package com.example.android.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class FirstActivity extends AppCompatActivity {
    private Button mButton= null;
    int order=0;
    String status="";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

    mButton = findViewById(R.id.goSecondActivity);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i= new Intent(view.getContext(),SecondActivity.class);
            startActivity(i);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.first, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId())
    {
        case R.id.settings:
            Toast.makeText(this, "Settings Clicked", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.about:
            Toast.makeText(this, "About Clicked", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.contact:
            Toast.makeText(this, "Contact Clicked", Toast.LENGTH_SHORT).show();
            return true;
    }
    return super.onOptionsItemSelected(item);
  }
}

SecondActivity.java

package com.example.android.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by NIHIR on 10-01-2018.
 */

public class SecondActivity extends AppCompatActivity{

private Button mButton = null;
private Button mDetailButton = null;
private TextView mSelectedView = null;
private Bundle extras = null;

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

    mButton = findViewById(R.id.goFirstActivity);

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(view.getContext(), FirstActivity.class);
            startActivity(i);
        }
    });
    mDetailButton = findViewById(R.id.goDetailActivity);

    mDetailButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(view.getContext(), DetailActivity.class);
            i.putExtra("KeyForSending", "Some Data from Second Activity");
            startActivity(i);
        }
    });
    mSelectedView = findViewById(R.id.userSelection);
    extras = getIntent().getExtras();
    if(extras!=null){
        String myValue= extras.getString("KeyForReturning");
        if(myValue!=null){
            mSelectedView.setText(myValue);
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.first, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId())
    {
        case R.id.settings:
            Toast.makeText(this, "Settings Clicked", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.about:
            Toast.makeText(this, "About Clicked", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.contact:
            Toast.makeText(this, "Contact Clicked", Toast.LENGTH_SHORT).show();
            return true;
    }
    return super.onOptionsItemSelected(item);
}
}

DetailActivity.java

package com.example.android.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * Created by NIHIR on 10-01-2018.
 */

public class DetailActivity extends AppCompatActivity {

private Spinner mSpinner= null;
private Bundle extras = null;
private Button mReturnButton= null;
private Button mPerformButton= null;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);

    extras = getIntent().getExtras();

    if(extras!=null){
        String detailValue = extras.getString("KeyForSending");
        if( detailValue != null){
            Toast.makeText(this, detailValue,Toast.LENGTH_SHORT).show();
        }
    }

    mSpinner = findViewById(R.id.spinnerSelector);
    mReturnButton= findViewById(R.id.returnToSecondActivity);
    mReturnButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent returnIntent = new Intent(view.getContext(),SecondActivity.class);
            String mySelection = mSpinner.getSelectedItem().toString();
            returnIntent.putExtra("KeyForReturning", mySelection);
            startActivity(returnIntent);
        }
    });
}
}

如何将SecondActivity中的TextView更改为第三个活动中的微调器选择,这样即使我回到FirstActivity也不会改变?

2 个答案:

答案 0 :(得分:0)

那是因为当您从 DetailActivity 转到 SecondActivity 时,您将值传回去,这就是为什么它是显示在 SecondActivity 中,但当您关闭它并返回 FirstActivity 然后再转到 SecondActivity 时,您并未传递任何意图。

您知道,一旦您关闭某项活动(通过完成()后退按钮,它就会丢失所有数据(当然,除非您重新开始)将它保存在某个地方)并重新创建为新实例。

解释为什么你没有得到你想要的东西。要保留数据,请将其保存在 SharedPreferences 或您喜欢的任何地方,然后只要您愿意,它就可以使用。

答案 1 :(得分:0)

在SecondActivity中,保存实例状态,而不是从extra获取值,从onCreate方法中的savedInstanceState获取它。

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putString("KeyForReturning", mSelectedView.getText());

    // call superclass to save any view hierarchy
    super.onSaveInstanceState(outState);
}



if(savedInstanceState != null){
  String myValue = savedInstanceState.getString("KeyForReturning");
  if(myValue != null){
    mSelectedView.setText(myValue);
  }
}