当值存储在共享首选项活动中时第二个活动打开时带有意图值

时间:2015-08-07 16:22:12

标签: android android-intent android-activity sharedpreferences

在我的应用程序中,我想启动将值和意图存储到下一个活动的活动。然后当我下次打开应用程序时如果共享首选项中存在值,则第一个活动将无法打开。我尝试了很多逻辑,但没有任何对我有用。请帮助我。

package com.jsm.irs;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {

 // UI References
private EditText textEtxt;
private Button saveButton;
private Button activity2Button;

private String text;

private SharedPreference sharedPreference;

Activity context = this;

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

    sharedPreference = new SharedPreference();

    findViewsById();

    saveButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            text = textEtxt.getText().toString();

            // Hides the soft keyboard
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(textEtxt.getWindowToken(), 0);

            // Save the text in SharedPreference
            sharedPreference.save(context, text);
            Toast.makeText(context,
                    getResources().getString(R.string.saved),
                    Toast.LENGTH_LONG).show();
        }
    });

    activity2Button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, SecondActivity.class);
            // Start next activity
            startActivity(intent);
        }
    });
}

private void findViewsById() {
    textEtxt = (EditText) findViewById(R.id.etxt_text);
    saveButton = (Button) findViewById(R.id.button_save);
    activity2Button = (Button) findViewById(R.id.button_second_activity);
}

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

这是共享偏好类

package com.jsm.irs;

 import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.preference.PreferenceManager;

 public class SharedPreference {

 public static final String PREFS_NAME = "AOP_PREFS";
 public static final String PREFS_KEY = "AOP_PREFS_String";

public SharedPreference() {
    super();
}

 public void save(Context context, String text) {
    SharedPreferences settings;
    Editor editor;

    //settings = PreferenceManager.getDefaultSharedPreferences(context);
    settings = context.getSharedPreferences(PREFS_NAME,            Context.MODE_PRIVATE); //1
    editor = settings.edit(); //2

    editor.putString(PREFS_KEY, text); //3

    editor.commit(); //4
}

public String getValue(Context context) {
    SharedPreferences settings;
    String text;

    //settings = PreferenceManager.getDefaultSharedPreferences(context);
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    text = settings.getString(PREFS_KEY, null);
    return text;
}

public void clearSharedPreference(Context context) {
    SharedPreferences settings;
    Editor editor;

    //settings = PreferenceManager.getDefaultSharedPreferences(context);
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    editor = settings.edit();

    editor.clear();
    editor.commit();
}

public void removeValue(Context context) {
    SharedPreferences settings;
    Editor editor;

    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    editor = settings.edit();

    editor.remove(PREFS_KEY);
    editor.commit();
}    
}

第二个活动类

package com.jsm.irs;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import com.jsm.irs.SharedPreference;
import android.view.Menu;
import android.view.inputmethod.InputMethodManager;


 public class SecondActivity extends Activity {

// UI References
private TextView textTxt;

private String text;

private SharedPreference sharedPreference;

Activity context = this;

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

    sharedPreference = new SharedPreference();

    findViewsById();

    //Retrieve a value from SharedPreference
    text = sharedPreference.getValue(context);
    textTxt.setText(text);

}

private void findViewsById() {
    textTxt = (TextView) findViewById(R.id.textView1);
}

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

2 个答案:

答案 0 :(得分:0)

试试这个:

    package com.jsm.irs;
    import android.support.v7.app.ActionBarActivity;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    public class MainActivity extends ActionBarActivity {

     // UI References
    private EditText textEtxt;
    private Button saveButton;
    private Button activity2Button;

    private String text;

    private SharedPreference sharedPreference;

    Activity context = this;

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

        sharedPreference = new SharedPreference();

        findViewsById();
        if(sharedPreference.getValue(this).equals("YOUR_TEXT")){
         startActivity(new Intent(MainActivity.this, SecondActivity.class));
         finish();
         }
        saveButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                text = textEtxt.getText().toString();

                // Hides the soft keyboard
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(textEtxt.getWindowToken(), 0);

                // Save the text in SharedPreference
                sharedPreference.save(context, text);
                Toast.makeText(context,
                        getResources().getString(R.string.saved),
                        Toast.LENGTH_LONG).show();
            }
        });

        activity2Button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SecondActivity.class);
                // Start next activity
                startActivity(intent);
            }
        });
    }
private void findViewsById() {
    textEtxt = (EditText) findViewById(R.id.etxt_text);
    saveButton = (Button) findViewById(R.id.button_save);
    activity2Button = (Button) findViewById(R.id.button_second_activity);
}

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

答案 1 :(得分:0)

当您将值保存到.Preference时,Toast是否显示保存的值?将值保存到首选项后是否有任何错误?因为如果值没有正确保存,那么null将在您获取时返回。

解决方案: 从MainActivity onCreate方法读取SharedPreference值,如果值存在,则启动Activity2,然后启动Activity1

public class MainActivity extends ActionBarActivity {

 // UI References
private EditText textEtxt;
private Button saveButton;
private Button activity2Button;

private String text;

private SharedPreference sharedPreference;

// Activity context = this;

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

    sharedPreference = new SharedPreference();
// Read SharedPreference Value 
String prefValue = shared preference.get value(Main activity.this); 
If(prefValue.equals(wantedValue) || prefValue != null)
{
Intent openActivity2 = new Intent(Main activity.this, Second activity.class); 
startActivity(openActivity2); 
}

    saveButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            text = textEtxt.getText().toString();

            // Hides the soft keyboard
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(textEtxt.getWindowToken(), 0);

            // Save the text in SharedPreference
            sharedPreference.save(context, text);
            Toast.makeText(context,
                    getResources().getString(R.string.saved),
                    Toast.LENGTH_LONG).show();
        }
    });

    activity2Button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, SecondActivity.class);
            // Start next activity
            startActivity(intent);
        }
    });
}

private void findViewsById() {
    textEtxt = (EditText) findViewById(R.id.etxt_text);
    saveButton = (Button) findViewById(R.id.button_save);
    activity2Button = (Button) findViewById(R.id.button_second_activity);
}