로그인 활동에서 인 텐트를 보내고 사용자 활동 (첨부 파일 참조)에 대한 인 텐트를 수신하는 데 성공했지만 다른 페이지에서 탐색 한 다음 내 UserAcitivy로 돌아가는 경우이 앱을 사용할 수 있습니다. "null 값"결과, 다른 페이지에서 탐색하는 경우에도 사용자 작업에 데이터를 유지하려고합니다. 도와주세요. 고맙습니다! 여기 데이터베이스에서 데이터 활동을 유지하는 방법
는 .. 내 코드의 LoginActivityEN이 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();
}
UserActivity.java
공용 클래스 UserActivity 확장 AppCompatActivity {
private TextView greetingTextView;
private TextView totpoints;
private TextView totshare;
private Button btnLogOut;
private ImageView cardshow;
private ImageView bmbc;
private TextView bmbc_text;
SharedPreferences sharedPreferences;
// Session Manager Class
SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_user);
// Session class instance
session = new SessionManager(getApplicationContext());
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(UserActivity.this, Home.class);
startActivity(intent1);
break;
case R.id.navigation_card:
break;
case R.id.navigation_price:
Intent intent3 = new Intent(UserActivity.this, PriceActivity.class);
startActivity(intent3);
break;
case R.id.navigation_more:
Intent intent4 = new Intent(UserActivity.this, PriceActivity.class);
startActivity(intent4);
break;
}
return false;
}
});
greetingTextView = (TextView) findViewById(R.id.greeting_text_view);
totpoints = (TextView) findViewById(R.id.au_tpresult);
totshare = (TextView) findViewById(R.id.au_tsresult);
btnLogOut = (Button) findViewById(R.id.logout_button);
cardshow = (ImageView) findViewById(R.id.card_stack);
Intent intent = getIntent();
String user = intent.getStringExtra("customers_firstname");
String user1 = intent.getStringExtra("customers_lastname");
String user2 = intent.getStringExtra("reward_points");
String user3 = intent.getStringExtra("NoShares");
String user4 = intent.getStringExtra("CardType_ID");
String user5 = intent.getStringExtra("Card_No");
greetingTextView.setText(user + " " + user1);
totpoints.setText(user2);
totshare.setText(user3);
if (user4 == (null)) {
((ImageView) findViewById(R.id.card_stack)).setImageResource(R.color.transparent);
} else if (user4.equals("0")) {
((ImageView) findViewById(R.id.card_stack)).setImageResource(R.drawable.thar_silver);
} else if (user4.equals("1")) {
((ImageView) findViewById(R.id.card_stack)).setImageResource(R.drawable.thar_gold);
}
// Progress dialog
btnLogOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
session.logoutUser();
Toast.makeText(getApplicationContext(), "Session Ended", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), LoginActivityEN.class);
startActivity(i);
}
});
session.checkLogin();
//CARD NUMB
안녕하세요,이 경우 모든 데이터를 공유 환경 설정에 저장하고 원하는 곳에 액세스하십시오. 공유 환경 설정에서 의도 저장소를 통과하는 대신 성공적인 로그인 후 홈 페이지에서 검색하십시오. – Shanmugam
@Shanmugam이 말한 것으로 작업을 수행하거나 onsaveinstancestate에 변수 값을 저장하고 on create 메소드에서 복원 할 수 있습니다. 다시 보려는 뷰가 일부 메소드의 로컬 변수가 아닌 클래스 필드인지 확인하십시오. :-) –