0

In SIGNIN Activity Upon clicking Sign In button data is retrieved from database in a separate background class (extending asynctask). onPostExecute I start "Navigation Drawer Activity" and send that data using intent to Navigation Drawer Activity. In "DrawerActivity" I am receiving that data and display it in a textView. I used sharedPreferences so that after successful login SIGNIN activity does not open again, so when I launch the activity second time textView has null value because no extras were passed. How to resolve this and store the value of textView permanently.

Background Class

@Override
protected void onPostExecute(final String result) {
final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(ctx);
        builder.setMessage("Login Successfull");
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                if (!result.contains("FALSE")) {
                    Intent i = new Intent(ctx, DrawerActivity.class);
                    Bundle b = new Bundle();
                    b.putString("name", namejsn);
                    b.putString("email", emailjsn);
                    i.putExtra("Login", b);
                    ((Activity) ctx).finish();
                    ctx.startActivity(i);
                }

DrawerActivity

public class DrawerActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

static String email;
static String firstName;
static String lastname;
static String profile;
ImageView imageViewHeader;
private int PICK_IMAGE_REQUEST = 1;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    SharedPreferences sharedPreferences = getSharedPreferences("MyLogin.txt", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("FirstLogin", true);
    editor.commit();

        Intent i = getIntent();
    if (i.hasExtra("Login")) {
        Bundle b = i.getBundleExtra("Login");
        firstName = b.getString("name");
        email = b.getString("email");
        Log.d("DrawerActivityLogin", email + firstName);
    }

      TextView tv1 = (TextView) v.findViewById(R.id.txtheader)
      TextView tv2 = (TextView) v.findViewById(R.id.txtheadr2);
      tv1.setText(firstName)
      tv2.setText(email);
Bilal Ahmed
  • 185
  • 1
  • 3
  • 12
  • 2
    Store it in SharedPreferences or a local DB then you can check that boolean variable to know if you get it from the Intent Extras or the SharedPrefs/DB. – codeMagic Jan 06 '17 at 21:07
  • @codeMagic can we use shared preferences in background class which is extending asyntask. – Bilal Ahmed Jan 06 '17 at 21:10
  • 1
    You will need a `Context` but sure. Probably better to pass a callback to the Activity and set/retrieve them there – codeMagic Jan 06 '17 at 21:13
  • @codeMagic yes Context will work, I'm trying to pass callback good idea – Bilal Ahmed Jan 06 '17 at 21:18
  • The bottom part of [this answer](http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648) should help you with that if you need it. – codeMagic Jan 06 '17 at 21:43

0 Answers0