Search code examples
androidandroid-studioretrofitretrofit2getter-setter

Calling data from server using retrofit on login screen and want show data on other activity


I am using retrofit library I want to call all data on login screen and if login successful then show data in next activity through same getter setter. Is this possible?

public void onResponse(Call<TaskResponse> call, Response<TaskResponse> response) 
{
    int statusCode = response.code();
    String checking = String.valueOf(response.body().getCode());
    if (checking.equals("0"))
        Toast.makeText(getApplicationContext(),"Invalid username or Password",Toast.LENGTH_LONG).show();
    else if (checking.equals("2"))
        Toast.makeText(getApplicationContext(),"Invalid Company Code",Toast.LENGTH_LONG).show();
    else if (checking.equals("1")) {
        Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_SHORT).show();

        List<Task> test;
        test = new ArrayList<Task>();
        test = response.body().getTasks();

        Intent intent = new Intent(LoginActivity.this, EmpTaskActivity.class);
        intent.putParcelableArrayListExtra("test", (ArrayList<? extends Parcelable>) test);
        startActivity(intent);
    }

   //  recyclerView.setAdapter(new MoviesAdapter(movies, R.layout.list_item_movie, getApplicationContext()));
}

Solution

  • At the login success (In your LoginActivity).

    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    intent.putExtra("DESIRE_KEY",loginModelUsedToCallRetrofitAPI);
    startActivity(intent);
    

    And at the MainActivity (the second activity you want to call after successful login)

    Intent intent = getIntent();
    LoginModelUsedToCallRetrofitAPI loginModelUsedToCallRetrofitAPI = intent.getSerializableExtra("DESIRE_KEY");
    

    This should do what you want!