Android Activity Lifecycle called twice when starting new Activity












3















I am basically starting new Activity which contains a WebView. But this WebView Activity's lifecycle called twice when first run. When destroy this activity and reopen again everything works fine.



Activity A runs



Act A -> onCreate

Act A -> onStart

Act A -> onResume


Start new activity with



startActivity(new Intent(this, WebViewActivity.class));


Activity B (WebView Activity) runs



Act A -> onPause

Act B -> onCreate

Act B -> onStart

Act B -> onResume


Log info may be cause this situation



W/WindowManager: Attempted to set replacing window on non-existing app token Token{1a4a5a ActivityRecord{219a05 u0 ui.activity.WebViewActivity t130}}


then continue with



Act B -> onPause

Act B -> onStop

Act B -> onDestroy

Act B -> onCreate

Act B -> onStart

Act B -> onResume

Act A -> onStop


I have tested this situation with many devices



Xiaomi mi a2 Lite Api 27-> error happens
Lg g4 Api 24 -> error happens
Huawei Nexus 6P Api 27-> error happens
Huawei Mate 10 Lite Api 26 -> error happens

Emulator Api 21 -> error not happens
Xiomi mi a2 Api 27 -> error not happens


is it a device bug? or OS bug? How can I fix it? if anyone can help, I will be appreciate.



My WebView Activity Code



public class WebViewActivity extends BaseActivity implements ViewClickHandler {

public static final String TAG = WebViewActivity.class.getSimpleName();

public static final String URL = "https://www.google.com";

ActivityWebViewBinding binding;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

binding = DataBindingUtil.setContentView(this, R.layout.activity_web_view);
binding.setClickHandler(this);
binding.setLifecycleOwner(this);

setToolbar(binding.toolbar, true, R.string.terms_of_service);

initWebview();
binding.webView.loadUrl(URL);

Log.d(TAG, "onCreate");
}

private void initWebview() {

binding.webView.setWebViewClient(new WebViewClient(){

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}

@Override
public void onPageStarted(
WebView view, String url, Bitmap favicon) {

try {
// showProgressDialog();
} catch (Exception ignore) {

}


}

@Override
public void onPageFinished(WebView view, String url) {

try {
// hideProgressDialog();
} catch (Exception ignore) {

}


}

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
//Your code to do
try {
// hideProgressDialog();
} catch (Exception ignore) {

}
}

});

binding.webView.setHorizontalScrollBarEnabled(false);
binding.webView.getSettings().setJavaScriptEnabled(true);
binding.webView.getSettings().setUseWideViewPort(true);
binding.webView.setInitialScale(1);
binding.webView.getSettings().setLoadWithOverviewMode(true);

}

@Override
public void onClick(View view) {

}

@Override
public void onCheckChange(View view, boolean isChecked) {

}


@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart");
}

@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}

@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}

@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}









share|improve this question




















  • 1





    can you post WebViewActivity code

    – sasikumar
    Nov 21 '18 at 8:59











  • @sasikumar, can you check again, posted webview code

    – ysfcyln
    Nov 21 '18 at 9:13
















3















I am basically starting new Activity which contains a WebView. But this WebView Activity's lifecycle called twice when first run. When destroy this activity and reopen again everything works fine.



Activity A runs



Act A -> onCreate

Act A -> onStart

Act A -> onResume


Start new activity with



startActivity(new Intent(this, WebViewActivity.class));


Activity B (WebView Activity) runs



Act A -> onPause

Act B -> onCreate

Act B -> onStart

Act B -> onResume


Log info may be cause this situation



W/WindowManager: Attempted to set replacing window on non-existing app token Token{1a4a5a ActivityRecord{219a05 u0 ui.activity.WebViewActivity t130}}


then continue with



Act B -> onPause

Act B -> onStop

Act B -> onDestroy

Act B -> onCreate

Act B -> onStart

Act B -> onResume

Act A -> onStop


I have tested this situation with many devices



Xiaomi mi a2 Lite Api 27-> error happens
Lg g4 Api 24 -> error happens
Huawei Nexus 6P Api 27-> error happens
Huawei Mate 10 Lite Api 26 -> error happens

Emulator Api 21 -> error not happens
Xiomi mi a2 Api 27 -> error not happens


is it a device bug? or OS bug? How can I fix it? if anyone can help, I will be appreciate.



My WebView Activity Code



public class WebViewActivity extends BaseActivity implements ViewClickHandler {

public static final String TAG = WebViewActivity.class.getSimpleName();

public static final String URL = "https://www.google.com";

ActivityWebViewBinding binding;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

binding = DataBindingUtil.setContentView(this, R.layout.activity_web_view);
binding.setClickHandler(this);
binding.setLifecycleOwner(this);

setToolbar(binding.toolbar, true, R.string.terms_of_service);

initWebview();
binding.webView.loadUrl(URL);

Log.d(TAG, "onCreate");
}

private void initWebview() {

binding.webView.setWebViewClient(new WebViewClient(){

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}

@Override
public void onPageStarted(
WebView view, String url, Bitmap favicon) {

try {
// showProgressDialog();
} catch (Exception ignore) {

}


}

@Override
public void onPageFinished(WebView view, String url) {

try {
// hideProgressDialog();
} catch (Exception ignore) {

}


}

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
//Your code to do
try {
// hideProgressDialog();
} catch (Exception ignore) {

}
}

});

binding.webView.setHorizontalScrollBarEnabled(false);
binding.webView.getSettings().setJavaScriptEnabled(true);
binding.webView.getSettings().setUseWideViewPort(true);
binding.webView.setInitialScale(1);
binding.webView.getSettings().setLoadWithOverviewMode(true);

}

@Override
public void onClick(View view) {

}

@Override
public void onCheckChange(View view, boolean isChecked) {

}


@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart");
}

@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}

@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}

@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}









share|improve this question




















  • 1





    can you post WebViewActivity code

    – sasikumar
    Nov 21 '18 at 8:59











  • @sasikumar, can you check again, posted webview code

    – ysfcyln
    Nov 21 '18 at 9:13














3












3








3








I am basically starting new Activity which contains a WebView. But this WebView Activity's lifecycle called twice when first run. When destroy this activity and reopen again everything works fine.



Activity A runs



Act A -> onCreate

Act A -> onStart

Act A -> onResume


Start new activity with



startActivity(new Intent(this, WebViewActivity.class));


Activity B (WebView Activity) runs



Act A -> onPause

Act B -> onCreate

Act B -> onStart

Act B -> onResume


Log info may be cause this situation



W/WindowManager: Attempted to set replacing window on non-existing app token Token{1a4a5a ActivityRecord{219a05 u0 ui.activity.WebViewActivity t130}}


then continue with



Act B -> onPause

Act B -> onStop

Act B -> onDestroy

Act B -> onCreate

Act B -> onStart

Act B -> onResume

Act A -> onStop


I have tested this situation with many devices



Xiaomi mi a2 Lite Api 27-> error happens
Lg g4 Api 24 -> error happens
Huawei Nexus 6P Api 27-> error happens
Huawei Mate 10 Lite Api 26 -> error happens

Emulator Api 21 -> error not happens
Xiomi mi a2 Api 27 -> error not happens


is it a device bug? or OS bug? How can I fix it? if anyone can help, I will be appreciate.



My WebView Activity Code



public class WebViewActivity extends BaseActivity implements ViewClickHandler {

public static final String TAG = WebViewActivity.class.getSimpleName();

public static final String URL = "https://www.google.com";

ActivityWebViewBinding binding;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

binding = DataBindingUtil.setContentView(this, R.layout.activity_web_view);
binding.setClickHandler(this);
binding.setLifecycleOwner(this);

setToolbar(binding.toolbar, true, R.string.terms_of_service);

initWebview();
binding.webView.loadUrl(URL);

Log.d(TAG, "onCreate");
}

private void initWebview() {

binding.webView.setWebViewClient(new WebViewClient(){

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}

@Override
public void onPageStarted(
WebView view, String url, Bitmap favicon) {

try {
// showProgressDialog();
} catch (Exception ignore) {

}


}

@Override
public void onPageFinished(WebView view, String url) {

try {
// hideProgressDialog();
} catch (Exception ignore) {

}


}

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
//Your code to do
try {
// hideProgressDialog();
} catch (Exception ignore) {

}
}

});

binding.webView.setHorizontalScrollBarEnabled(false);
binding.webView.getSettings().setJavaScriptEnabled(true);
binding.webView.getSettings().setUseWideViewPort(true);
binding.webView.setInitialScale(1);
binding.webView.getSettings().setLoadWithOverviewMode(true);

}

@Override
public void onClick(View view) {

}

@Override
public void onCheckChange(View view, boolean isChecked) {

}


@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart");
}

@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}

@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}

@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}









share|improve this question
















I am basically starting new Activity which contains a WebView. But this WebView Activity's lifecycle called twice when first run. When destroy this activity and reopen again everything works fine.



Activity A runs



Act A -> onCreate

Act A -> onStart

Act A -> onResume


Start new activity with



startActivity(new Intent(this, WebViewActivity.class));


Activity B (WebView Activity) runs



Act A -> onPause

Act B -> onCreate

Act B -> onStart

Act B -> onResume


Log info may be cause this situation



W/WindowManager: Attempted to set replacing window on non-existing app token Token{1a4a5a ActivityRecord{219a05 u0 ui.activity.WebViewActivity t130}}


then continue with



Act B -> onPause

Act B -> onStop

Act B -> onDestroy

Act B -> onCreate

Act B -> onStart

Act B -> onResume

Act A -> onStop


I have tested this situation with many devices



Xiaomi mi a2 Lite Api 27-> error happens
Lg g4 Api 24 -> error happens
Huawei Nexus 6P Api 27-> error happens
Huawei Mate 10 Lite Api 26 -> error happens

Emulator Api 21 -> error not happens
Xiomi mi a2 Api 27 -> error not happens


is it a device bug? or OS bug? How can I fix it? if anyone can help, I will be appreciate.



My WebView Activity Code



public class WebViewActivity extends BaseActivity implements ViewClickHandler {

public static final String TAG = WebViewActivity.class.getSimpleName();

public static final String URL = "https://www.google.com";

ActivityWebViewBinding binding;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

binding = DataBindingUtil.setContentView(this, R.layout.activity_web_view);
binding.setClickHandler(this);
binding.setLifecycleOwner(this);

setToolbar(binding.toolbar, true, R.string.terms_of_service);

initWebview();
binding.webView.loadUrl(URL);

Log.d(TAG, "onCreate");
}

private void initWebview() {

binding.webView.setWebViewClient(new WebViewClient(){

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}

@Override
public void onPageStarted(
WebView view, String url, Bitmap favicon) {

try {
// showProgressDialog();
} catch (Exception ignore) {

}


}

@Override
public void onPageFinished(WebView view, String url) {

try {
// hideProgressDialog();
} catch (Exception ignore) {

}


}

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
//Your code to do
try {
// hideProgressDialog();
} catch (Exception ignore) {

}
}

});

binding.webView.setHorizontalScrollBarEnabled(false);
binding.webView.getSettings().setJavaScriptEnabled(true);
binding.webView.getSettings().setUseWideViewPort(true);
binding.webView.setInitialScale(1);
binding.webView.getSettings().setLoadWithOverviewMode(true);

}

@Override
public void onClick(View view) {

}

@Override
public void onCheckChange(View view, boolean isChecked) {

}


@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart");
}

@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}

@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}

@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}






android android-lifecycle activity-lifecycle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 9:10







ysfcyln

















asked Nov 21 '18 at 8:53









ysfcylnysfcyln

2981817




2981817








  • 1





    can you post WebViewActivity code

    – sasikumar
    Nov 21 '18 at 8:59











  • @sasikumar, can you check again, posted webview code

    – ysfcyln
    Nov 21 '18 at 9:13














  • 1





    can you post WebViewActivity code

    – sasikumar
    Nov 21 '18 at 8:59











  • @sasikumar, can you check again, posted webview code

    – ysfcyln
    Nov 21 '18 at 9:13








1




1





can you post WebViewActivity code

– sasikumar
Nov 21 '18 at 8:59





can you post WebViewActivity code

– sasikumar
Nov 21 '18 at 8:59













@sasikumar, can you check again, posted webview code

– ysfcyln
Nov 21 '18 at 9:13





@sasikumar, can you check again, posted webview code

– ysfcyln
Nov 21 '18 at 9:13












1 Answer
1






active

oldest

votes


















0














I think this problem should be related to context. You should be using the context of the Activity. it seems you instantiated webview in DataBindingUtil class with the wrong context. Please pass the context that is WebViewActivity class to your Webview






share|improve this answer
























  • I am already using WebViewActivity context

    – ysfcyln
    Nov 21 '18 at 9:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408314%2fandroid-activity-lifecycle-called-twice-when-starting-new-activity%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I think this problem should be related to context. You should be using the context of the Activity. it seems you instantiated webview in DataBindingUtil class with the wrong context. Please pass the context that is WebViewActivity class to your Webview






share|improve this answer
























  • I am already using WebViewActivity context

    – ysfcyln
    Nov 21 '18 at 9:57
















0














I think this problem should be related to context. You should be using the context of the Activity. it seems you instantiated webview in DataBindingUtil class with the wrong context. Please pass the context that is WebViewActivity class to your Webview






share|improve this answer
























  • I am already using WebViewActivity context

    – ysfcyln
    Nov 21 '18 at 9:57














0












0








0







I think this problem should be related to context. You should be using the context of the Activity. it seems you instantiated webview in DataBindingUtil class with the wrong context. Please pass the context that is WebViewActivity class to your Webview






share|improve this answer













I think this problem should be related to context. You should be using the context of the Activity. it seems you instantiated webview in DataBindingUtil class with the wrong context. Please pass the context that is WebViewActivity class to your Webview







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 9:41









AmirAmir

159214




159214













  • I am already using WebViewActivity context

    – ysfcyln
    Nov 21 '18 at 9:57



















  • I am already using WebViewActivity context

    – ysfcyln
    Nov 21 '18 at 9:57

















I am already using WebViewActivity context

– ysfcyln
Nov 21 '18 at 9:57





I am already using WebViewActivity context

– ysfcyln
Nov 21 '18 at 9:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408314%2fandroid-activity-lifecycle-called-twice-when-starting-new-activity%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter