Search code examples
androidtextviewinterstitial

Why the previously entered text disappears from the TextView when Interstitial is closed


I have a microphone button with which the user enters text into the TextView with his speech, after every second click on the stop microphone button Interstitial appears, however, when Interstitial appears and when Interstitial is closed, the previously entered text disappears from the TextView, when the microphone start button is started again, the previously entered text reappears. What needs to be done to keep the text visible when the Interstitial is closed. I tried onAdClosed but failed. Can anyone specifically answer, thanks!

InterstitialAd.java

InterstitialAd.load(getApplicationContext(),
                        mContext.getString(R.string.admob_interstitial_id),
                        new AdRequest.Builder().build(),
                        new InterstitialAdLoadCallback() {

                            @Override
                            public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                                interstitialAd.show((Activity) mContext);
                            }

                            public void onAdClosed() {
                                returnedText.setVisibility(View.VISIBLE);
                            }
                        }
                );

The Start - Stop microphone button is located in the MainActivity.java and calls Interstitial from InterstitialAd.java

InterstitialLaunch interstitialLaunch = new InterstitialLaunch();
interstitialLaunch.inter_launched(MainActivity.this);

Solution

  • I think because after the ad, your fragment calls onCreate again (which follows with the normal lifecycle of fragment methods') creating the fragment from zero, if that's the case then what you need is to store UI data somewhere before showing the ad and fetch it later everytime onViewCreated (as an example but writing your code in onCreateView/onStateRestored will also do the job), you can store your data using a ViewModel or in SharedPreferences

    you can start reading about viewModel from here :

    ViewModel | Android Developers

    you can also start reading about sharedPerfreneces from here :

    Save key-value data |Android Developers