0

In the first activity of downloading data and sending them to the main activity

Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
Long userId = jsonResponse.getLong("id");
String username = jsonResponse.getString("username");
User user = new User(userId, username); 
//from server to  activity


myIntent.putExtra(MainActivity.USER_ID,userId);
myIntent.putExtra(MainActivity.USER, user);

startActivity(myIntent);

Then in the Main Activity picked up and transmitted to the Fragments

//class MainActivity
public static final String USER_ID = "user_id_key";
public static final String USER = "user_key";
private Long userId;

public User user,name;


//OnCreate
Intent intent = getIntent();
    if(intent !=null) {
        if(intent.hasExtra(USER_ID)) {
            userId = intent.getLongExtra(USER_ID, 0L);
        }
        if(intent.hasExtra(USER)) {
            user = (User) intent.getSerializableExtra(USER);

        }
    }


//setupViewPager
Bundle bundle = new Bundle();
bundle.putLong(USER_ID, userId);
bundle.putSerializable(USER, user);

In Fragments

private Long userId;
private User user;


//OnCreate
 Bundle bundle= getArguments();
    if(bundle !=null) {
        if(bundle.containsKey(USER_ID)) {
            userId = bundle.getLong(USER_ID, 0L);
        }
        if(bundle.containsKey(USER)) {
            user = (User) bundle.getSerializable(USER);
        }
 }

User Class

 package com.example.giftlist.giftlist.Data;

import java.io.Serializable;

public class User implements Serializable{

private Long id;

private String usename;

public User(Long id, String usename) {
    this.id = id;
    this.usename = usename;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getUsename() {
    return usename;
}

public void setUsename(String usename) {
    this.usename = usename;
}
}

My question is how to transfer the data to the next activity. AddActivity for example. AddActivity is run after you run the previous activity Login, Main and fragments

  • Are you trying to pass the new User object to the next activity? if yes then you the can use the Parcelable interface to wrap the object for sending to next activity. – GordonW Jan 23 '18 at 21:04

2 Answers2

0

My question is how to transfer the data to the next activity

Pass the extras along again.

//in MainActivity
Intent myIntent = new Intent(LoginActivity.this, AddActivity.class);
myIntent.putExtras(getIntent().getExtras());

Rather than pass your user's login credentials to every single in your app consider storing them with the PreferenceManager or something more secure as suggested here: What is the most appropriate way to store user settings in Android application

Sam
  • 84,460
  • 18
  • 172
  • 171
  • I need a simple solution at the moment. You can more describe more your way to transfer the extras along again. In Fragments I have the Adapter and from there I run AddActivity. – Dawid Frąk Jan 23 '18 at 21:16
  • Simplest solution? Pass `userId` and `user` to Adapter in a custom method like `setId()` and `setUserName()`, now you can put them in another Intent. But I really do recommend using the SharedPreference, it'll be easier in the long run. – Sam Jan 23 '18 at 21:31
0

1) By Intent:

from Activity Login:

Intent i = new Intent (getApplicationContext(),MainActivity.class);
i.putExtra("iEmail","Xyz");
i.putExtra("iPassword","44562");
startActivity(i);

in Activity Main

String Email= "";
String Password = "";
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
ID= extras.getString("iEmail");
Name= extras.getString("iPassword");

2) Use SQLite

from Activity Login:

String iEmail = "someEmail@mail.com";
String iPassword = "some_password";
SQLiteDatabase db;
db = openOrCreateDatabase("DB_Name", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Login(Email VARCHAR, Password VARCHAR);");
//drop table for clear all records and recreate
db.execSQL("DROP TABLE Login;");
db.execSQL("CREATE TABLE IF NOT EXISTS Login(Email VARCHAR, Password VARCHAR);");
db.execSQL("INSERT INTO Login VALUES('"+ iEmail +"','"+ iPassword +"',);");

in Activity Main or in any activity/service

String Email = "";
String Password = "";
SQLiteDatabase db;
db = openOrCreateDatabase("DB_Name", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Login(Email VARCHAR, Password VARCHAR);");
Cursor c=db.rawQuery("SELECT * FROM Login", null);
if(c.getCount()==0) {
   //do something if no records found
}
else{
   while(c.moveToNext()){
      Email = c.getString(0);
      PAssword = c.getString(1);
   }
}

Advantages of using SQLite:

  • store permanent data.
  • you can get it same data after app restart (very useful for autologin).
  • you can get that save data at any point for n number of times without repeating store procedure.
  • easily accessible in all activity or service at the same time.
Sagar Makhija
  • 613
  • 6
  • 9