我有这个应用程序,我成功从登录活动发送意图并接收用户活动的意图(请参阅附件),但当我从其他页面导航然后返回到我的UserAcitivy ie;主页,联系我们结果"空值",我想将数据保存在用户活动中,即使我从其他页面导航。请帮忙。谢谢!
这是我的代码..
公共类LoginActivityEN扩展了AppCompatActivity {
class Abc
arr_accessor :my_arrayobject
def initialize
self.my_arrayojbect = []
end
....
def update
self.my_arrayobject << parameter
end
end
p1 = Abc.new
puts p1.my_arrayobject
UserActivity.java
公共类UserActivity扩展了AppCompatActivity {
private static final String TAG = "LoginActivity";
private static final String URL_FOR_LOGIN = "http://192.168.13.40/android_login_example/login.php";
ProgressDialog progressDialog;
private EditText loginInputUsername, loginInputPassword;
Button btnlogin;
Button btnLinkSignup;
// Session Manager Class
SessionManager session;
//Storing Sessions
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_login_en);
// Session Manager
session = new SessionManager(getApplicationContext());
sharedPreferences = getSharedPreferences("my_prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("user_id", "password");
//Bottom Navigation
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(1);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_home:
Intent intent1 = new Intent(LoginActivityEN.this, Home.class);
startActivity(intent1);
break;
case R.id.navigation_card:
break;
case R.id.navigation_price:
Intent intent3 = new Intent(LoginActivityEN.this, PriceActivity.class);
startActivity(intent3);
break;
case R.id.navigation_more:
Intent intent4 = new Intent(LoginActivityEN.this, More.class);
startActivity(intent4);
}
return false;
}
});
//Login Activity
loginInputUsername = (EditText) findViewById(R.id.login_input_username);
loginInputPassword = (EditText) findViewById(R.id.login_input_password);
btnlogin = (Button) findViewById(R.id.btn_login);
btnLinkSignup = (Button) findViewById(R.id.registerbutton);
// Progress dialog
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loginUser(loginInputUsername.getText().toString(),
loginInputPassword.getText().toString());
}
});
//Register
btnLinkSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), RegisterEN.class);
startActivity(i);
}
});
}
private void loginUser( final String username, final String password) {
// Tag used to cancel the request
String cancel_req_tag = "login";
progressDialog.setMessage("Logging you in...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
URL_FOR_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
String user = jObj.getJSONObject("user").getString("customers_firstname");
String user1 = jObj.getJSONObject("user").getString("customers_lastname");
String user2 = jObj.getJSONObject("user").getString("reward_points");
String user3 = jObj.getJSONObject("user").getString("NoShares");
String user4 = jObj.getJSONObject("user").getString("CardType_ID");
String user5 = jObj.getJSONObject("user").getString("Card_No");
// Launch User activity
Intent intent = new Intent(LoginActivityEN.this, UserActivity.class);
intent.putExtra("customers_firstname", user);
intent.putExtra("customers_lastname", user1);
intent.putExtra("reward_points", user2);
intent.putExtra("NoShares", user3);
intent.putExtra("CardType_ID", user4);
intent.putExtra("Card_No", user5);
startActivity(intent);
finish();
if(username.trim().length() > 0 && password.trim().length() > 0){
session.createLoginSession("username", "password");
Toast.makeText(getApplicationContext(), "Session Active", Toast.LENGTH_SHORT).show();
}
} else {
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to login url
Map<String, String> params = new HashMap<String, String>();
params.put("username", username);
params.put("customers_password", password);
return params;
}
};
// Adding request to request queue
AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq,cancel_req_tag);
}
private void showDialog() {
if (!progressDialog.isShowing())
progressDialog.show();
}
private void hideDialog() {
if (progressDialog.isShowing())
progressDialog.dismiss();
}
答案 0 :(得分:0)
您可以使用许多方法。
持久存储(长时间,即使应用关闭后): 一个。使用SharedPreferences,b。使用Sqlite
其他方式是.to扩展Application类并将其存储在变量中,直到应用程序关闭为止。(短期,应用程序会话和关闭应用程序)
您的方法取决于您的数据生命周期