Omega
Omega

Reputation: 309

Activity finishes too quickly when loading for the first time at first launch

Am using this code to make an Activity load only when the app is launched for the first time but am having an issue, the Activity that is supposes to launch for the first time is a WebView, and while the WebView is loading the Activity finishes too quickly. This is the code am using to make the Activity load for the first time.

count = readSharedPreferenceInt("cntSP","cntKey");
if(count==0){
    Intent intent = new Intent();
    intent.setClass(MainActivity.this, TemporaryActivity.class);
    startActivity(intent);
    count++;
    writeSharedPreference(count,"cntSP","cntKey");
}

//Read from Shared Preferance
public int readSharedPreferenceInt(String spName,String key){
    SharedPreferences sharedPreferences = getSharedPreferences(spName,Context.MODE_PRIVATE);
    return tempInt = sharedPreferences.getInt(key, 0);
}     

//write shared preferences in integer
public void writeSharedPreference(int ammount,String spName,String key ){
    SharedPreferences sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putInt(key, ammount);
    editor.commit();
}

This is the code for the activity that loads for the first time

public class Reg_Status extends AppCompatActivity {
 Context a;
 private ProgressBar pBar;

/* renamed from: com.partners.app.Confmap.1 */
class C01561 extends WebViewClient {
    C01561() {
    }

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }

    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        Reg_Status.this.pBar.setVisibility(ProgressBar.VISIBLE);
    }

    public void onPageFinished(WebView view, String url) {
        Reg_Status.this.pBar.setVisibility(ProgressBar.INVISIBLE);
    }

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        view.loadUrl(Config.URL_INTERNET_ERROR);
        Reg_Status.this.pBar.setVisibility(ProgressBar.VISIBLE);
    }
}



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView((int) R.layout.activity_reg_status);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    this.pBar = (ProgressBar) findViewById(R.id.pBar);
    WebView webView = (WebView) findViewById(R.id.wView_reg);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new C01561());
    webView.loadUrl(Config.URL_REG);


}

Upvotes: 2

Views: 226

Answers (2)

vidulaJ
vidulaJ

Reputation: 1270

Okay. You have this method, onPageFinished(), implemented and do that snippet you are doing it inside onCreate() inside that method.

public void onPageFinished(WebView view, String url) {
    //You code, which you had put inside onCreate() should go here.
}

EDIT

Intent intent = new Intent(MainActivity.this, TemporaryActivity.class);
startActivity(intent);
...

NEW EDIT

public class C01561 extends WebViewClient {
    private Context context;
    C01561(Context context) {
        this.context = context;
    }

    public void onPageFinished(WebView view, String url) {
        Reg_Status.this.pBar.setVisibility(ProgressBar.INVISIBLE);

        Intent intent = new Intent(context, TemporaryActivity.class);
        startActivity(intent);
    }
}

And when you create an instance of WebViewClient make sure to pass the MainActivity context.

webView.setWebViewClient(new C01561(MainActivity.this));

Upvotes: 2

Sanjeet
Sanjeet

Reputation: 2403

Create one Handler to delay the start of the Second Activity.

new Handler().postDelayed(new Runnable() {


        @Override
        public void run() {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, TemporaryActivity.class);
            startActivity(intent);

        }
    }, TIME_OUT);

Now, call this handler in your if condition and set TIME_OUT accordingly.

Upvotes: 1

Related Questions