-3

I am beginner in App development and I have ready made source code. Please find below two files, I want to force user to login first then redirect to home page, If I change MainActivity to LoginActivity in IntroActivity file user getting login window but after login not redirect to MainActivity page. Please suggesst where exactly I need to change.

Code are:

File 1 :IntroActivity.java

package com.ristana.bullish.ui.activity;

import android.content.Intent;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ristana.bullish.R;
import com.ristana.bullish.entity.ApiResponse;
import com.ristana.bullish.manager.PrefManager;

import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import com.ristana.bullish.api.apiClient;
import com.ristana.bullish.api.apiRest;

public class IntroActivity extends AppCompatActivity {
    private ProgressBar intro_progress;
    private RelativeLayout activity_intro;
    private PrefManager prf;
    private TextView text_view_app_version;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        prf= new PrefManager(getApplicationContext());
        initView();
        Timer myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                // If you want to modify a view in your Activity
                IntroActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (prf.getString("LOGGED").contains("TRUE")){
                            checkAccount();
                        }else{
                            Intent intent = new Intent(IntroActivity.this,MainActivity.class);
                            startActivity(intent);
                        }
                    }
                });
            }
        }, 5000);
    }
    private  void initView(){
        setContentView(R.layout.activity_intro);
        this.intro_progress=(ProgressBar) findViewById(R.id.intro_progress);
        this.activity_intro=(RelativeLayout) findViewById(R.id.activity_intro);
        intro_progress.setVisibility(View.VISIBLE);
        this.text_view_app_version=(TextView) findViewById(R.id.text_view_app_version);
        try {
            PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(),0);
            String version = pInfo.versionName;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
    private void checkAccount() {

        intro_progress.setVisibility(View.VISIBLE);
        Retrofit retrofit = apiClient.getClient();
        apiRest service = retrofit.create(apiRest.class);
        Call<ApiResponse> call = service.check(prf.getString("ID_USER"),prf.getString("TOKEN_USER"));
        call.enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {

                if (response.isSuccessful()){
                    if (response.body().getCode()==200){
                        Intent intent = new Intent(IntroActivity.this,MainActivity.class);
                        startActivity(intent);
                    }else if(response.body().getCode()==500){
                        logout();
                        Intent intent = new Intent(IntroActivity.this,MainActivity.class);
                        startActivity(intent);
                    }else {
                        Intent intent = new Intent(IntroActivity.this,MainActivity.class);
                        startActivity(intent);
                    }
                }else{
                    Intent intent = new Intent(IntroActivity.this,MainActivity.class);
                    startActivity(intent);
                }

            }
            @Override
            public void onFailure(Call<ApiResponse> call, Throwable t) {
                Intent intent = new Intent(IntroActivity.this,MainActivity.class);
                startActivity(intent);
                intro_progress.setVisibility(View.INVISIBLE);

            }
        });

    }
    public      void logout(){
        PrefManager prf= new PrefManager(getApplicationContext());
        prf.remove("ID_USER");
        prf.remove("SALT_USER");
        prf.remove("TOKEN_USER");
        prf.remove("NAME_USER");
        prf.remove("TYPE_USER");
        prf.remove("USERNAME_USER");
        prf.remove("URL_USER");
        prf.remove("LOGGED");
        Toast.makeText(getApplicationContext(),getString(R.string.message_logout_desibaled),Toast.LENGTH_LONG).show();
    }
    @Override
    public  void onPause(){
        super.onPause();
        finish();
    }
}

File 2: activity_intro.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_intro"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.ristana.bullish.ui.activity.IntroActivity">
    <ProgressBar
        android:progressDrawable="@drawable/progress_yellow"
        android:indeterminateDrawable="@drawable/progress_yellow"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:id="@+id/intro_progress" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/logo_app"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="5dp"
            android:textSize="20dp"
            android:textStyle="bold"
            android:layout_centerInParent="true"
            android:textColor="@color/primary_text"
            android:textAlignment="center"
            android:text=" "
            android:id="@+id/text_view_app_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="8dp"
            android:textSize="18dp"
            android:layout_centerInParent="true"
            android:textColor="@color/primary_text"
            android:textAlignment="center"
            android:text=" "
            android:id="@+id/text_view_app_version"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <ImageView
        android:layout_width="130dp"
        android:layout_height="130dp"
        app:srcCompat="@drawable/logo"
        android:id="@+id/logo_app"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Thanks in advance.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

Just check out what is the response code return from retrofit call. Best way would be debug the code. On successful response/failure, code is written. But still need to know whether code reaching there. If it does not reach there , the steps would not be carried out. Hope that helps.

litelite
  • 2,857
  • 4
  • 23
  • 33
priya raj
  • 362
  • 2
  • 8
0

As it was stated in the comments, I guess you don't not how this code works, at all, so I will try to simplified for you.

First you are using retrofit, which is an excellent tool to use for getting and posting data to servers without all the trouble (google that and you will find out more). Then you are using PrefManager that is used to store some key/value pairs in application storage so you can retrive them when needed. (What are the PreferenceManager and SharedPreference classes used for in Android? here's a link that will clear that).

And to the part that you asked about, you are calling retrofits on respons, and if it responsed with success (means that the login data was send to server, tested, and it's ok), then you are creating new Intent telling to swipe IntoActivity.this (your currentactivity) with MainActivity.class and with the startActivity(intent) it redirects you to MainActivity. You can't just "change" the MainActivty with LoginActivity and wait to be redirected to Main. You can redirect the intent to LoginActivity and then redirect it again to MainActivity (as I don't know what you are trying to do here). And don't forget, any new activity should be stated in the Manifest.

I will suggest reading more about android and it's basics before you try to take on this kind of code as you will be lost.

Good luck coding.

Claire
  • 60
  • 11