Fragment in activity startup taking more time than expected - java

I am a beginner-intermediate android developer. I started working with fragments in my this app. The structure of the app is:
Main activity where some important links are embedded with buttons
On clicking button from main activity, some DB tasks completed (with Room Library so all tasks are using AsyncTask) and new activity opens with link in intent extra
On getting the link, the new activity-2 adds a fragment (in itself) and also performs some DB tasks in background and then opens the link in webview of fragment.
The problem is, from 1-2 it is taking merely 0.1-0.15 seconds while on starting the task 3, it is taking 0.3-0.45 seconds so on clicking from main activity, user is getting the link opened in fragment (which has webview) in about 0.6 seconds which is making feel like app is freezing.
Here are some codes:
Activity-2:
#Override
protected void onCreate(Bundle savedInstanceState) {
prefSingleton = PrefSingleton.getInstance();
if (prefSingleton.getStorage().getBoolean(Constants.STORAGE_ENABLE_NIGHTMODE,false)){
setTheme(R.style.DarkTheme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_website_view);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
closeWebview = toolbar.findViewById(R.id.closeWebview);
downArrow = toolbar.findViewById(R.id.downArrow);
allTabs = toolbar.findViewById(R.id.allTabs);
searchIcon = toolbar.findViewById(R.id.search);
clearEditText = toolbar.findViewById(R.id.clearEditText);
selectedSEImage = toolbar.findViewById(R.id.selectedSEImage);
searchEditText = toolbar.findViewById(R.id.searchEditText);
searchEngineFrame = toolbar.findViewById(R.id.searchEngine);
searchBarLayout = toolbar.findViewById(R.id.searchBarLayout);
toolbarTitle = toolbar.findViewById(R.id.toolBarTitle);
navFrame = findViewById(R.id.navFrame);
navRecView = findViewById(R.id.navRecView);
if (prefSingleton.getStorage().getBoolean(Constants.STORAGE_ENABLE_NIGHTMODE,false)){
navFrame.setBackgroundColor(getResources().getColor(R.color.night_mode_toolbar));
}
//Activity toolbar views
closeWebview.setOnClickListener(this);
closeWebview.setOnLongClickListener(this);
downArrow.setOnClickListener(this);
allTabs.setOnClickListener(this);
searchIcon.setOnClickListener(this);
clearEditText.setOnClickListener(this);
searchEngineFrame.setOnClickListener(this);
searchEditText.setOnClickListener(this);
searchEditText.setOnKeyListener(this);
//Intent from main activity
Intent i = getIntent();
String urlType = i.getStringExtra(String.valueOf(EnumVal.SiteInfoToSend.TYPE));
final String url = i.getStringExtra(String.valueOf(EnumVal.SiteInfoToSend.URL));
String title = i.getStringExtra(String.valueOf(EnumVal.SiteInfoToSend.TITLE));
String searchedText = i.getStringExtra(String.valueOf(EnumVal.SiteInfoToSend.SEARCHED_TEXT));
fragCounter = 0;
fragTags = new ArrayList<>();
//Fragment opening
if (savedInstanceState == null){
openFragment(url, EnumVal.FragStatus.NEW, null);
}
//AdMob Ads
if (!BuildConfig.PAID_VERSION){
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_webview));
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
if(ConsentInformation.getInstance(WebsiteView.this).getConsentStatus() ==
ConsentStatus.NON_PERSONALIZED){
Bundle extras = new Bundle();
extras.putString("npa", "1");
mInterstitialAd.loadAd(new AdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class,extras).build());
} else {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}
});
if(ConsentInformation.getInstance(WebsiteView.this).getConsentStatus() == ConsentStatus.NON_PERSONALIZED){
Bundle extras = new Bundle();
extras.putString("npa", "1");
mInterstitialAd.loadAd(new AdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class,extras).build());
} else {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}
//Toolbar search edit text will be enabled in below case
if (urlType.equals(EnumVal.Type.SEARCHED_TEXT.toString())){
searchBarLayout.setVisibility(View.VISIBLE);
searchIcon.setImageResource(R.drawable.close_icon);
searchEditText.setText(searchedText);
searchEditText.clearFocus();
searchEditText.setCursorVisible(false);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
if (searchEditText.hasFocus())
searchEditText.setCursorVisible(true);
}
searchEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
clearEditText.setVisibility(View.VISIBLE);
}
});
//Navigation view in toolbar
navRecView.setLayoutManager(new LinearLayoutManager(this));
mViewModel = ViewModelProviders.of(this).get(DbViewModel.class);
mViewModel.getSitesWithClicksByIsShown().observe(this, new Observer<List<SitesWithClicks>>() {
#Override
public void onChanged(#Nullable List<SitesWithClicks> sitesWithClicks) {
if (sitesWithClicks!= null && sitesWithClicks.size()>0){
int reArrSite = prefSingleton.getStorage().getInt(Constants.STORAGE_REARRANGESITE_NAV,
Constants.SORTING_PRIORITY);
sitesWithClicks = Utility.sortSitesData(reArrSite, sitesWithClicks);
navAdapter = new ListRecViewAdapter(EnumVal.DialogType.NAVBAR_ITEMS,
sitesWithClicks, WebsiteView.this);
navRecView.setAdapter(navAdapter);
}
}
});
}
Open-fragment method:
public void openFragment(String url, EnumVal.FragStatus fragStatus, String toOpenTag){
if(navFrame.getVisibility() == View.VISIBLE)
downArrow.performClick();
if(fragStatus == EnumVal.FragStatus.NEW){
String fragTag = getNextFragTag();
WebsiteViewFragment fragment = WebsiteViewFragment.newInstance(url, fragTag);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if(fragCounter == 1){
fragmentTransaction.replace(R.id.fragment, fragment, fragTag);
currentFrag = fragTag;
} else {
if(getSupportFragmentManager().findFragmentByTag(currentFrag) != null){
fragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
fragmentTransaction.hide(getSupportFragmentManager().findFragmentByTag(currentFrag));
}
fragmentTransaction.add(R.id.fragment, fragment, fragTag);
currentFrag = fragTag;
}
fragmentTransaction.commit();
fragTags.add(new TabDetail(fragTag,"Tab - 1",null));
} else if (fragStatus == EnumVal.FragStatus.OLD){
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if(getSupportFragmentManager().findFragmentByTag(currentFrag) != null){
fragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
fragmentTransaction.hide(getSupportFragmentManager().findFragmentByTag(currentFrag));
}
fragmentTransaction.show(getSupportFragmentManager().findFragmentByTag(toOpenTag));
fragmentTransaction.commit();
currentFrag = toOpenTag;
}
}
And Fragment has just Webview in it where webview is loading lot of websettings. But thing is reaching till fragment is taking time ,like user clicks from main activity and after about 0.6 seconds, fragment is opening while activity which has fragment is taking just 0.1 second to open so this time is (maybe) related to attaching fragment or something?
Can anyone please explain me, where am I making mistake?
I had some doubt regarding this:
Is the problem webview (in fragment), which has a bunch of websettings ?
Is attaching fragment to activity-2 taking more time?
Is UI part of activity-2 taking time (like toolbar setup which has 4 buttons) and after that fragment is attaching to it, which is resulting in more time consumption ?
Or it is a normal situation ?
Can someone, please, explain me the way to make it to <0.2 seconds for whole tasks? Thanks in advance.

Related

Data resets when bottom navigation views are clicked

I have a bottom navigation view on my weather app containing 3 panels(Today, hourly & Daily). My activity hosts 3 fragments for the 3 panels. Currently, if I search for any city on the today fragment, it gives the data for such cities. Now the problem is that if I click on any bottom nav view(down), it resets the data displayed on the today fragment.
Here is an illustration of the issue:
Data is displayed after a city is searched(the part with a red tick):
Data goes on reset after clicking these bottom nav views(the part with a red tick):
I want the data to remain intact irrespective of clicking those nav views.
I tried using https://stackoverflow.com/a/60201555/16020235 suggestion. But it failed with this
exception:
java.lang.IllegalArgumentException: No view found for id 0x7f0a0116 (com.viz.lightweatherforecast:id/my_nav) for fragment ThirdFragment{90bc0de} (8e129d17-010d-41dc-9311-82e273b4e522 id=0x7f0a0116 tag=3)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:513)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3134)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3068)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:501)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:210)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
at android.app.Activity.performStart(Activity.java:7157)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3037)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1861)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6819)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)
I/weatherforecas: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
I find it hard to implement his code and the remaining suggestions on that question are written in kotlin.
Please how can I resolve this?
Here are my codes:
my_nav.xml(navigation layout):
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/my_nav"
app:startDestination="#id/firstFragment">
<fragment
android:id="#+id/firstFragment"
android:name="com.viz.lightweatherforecast.FirstFragment"
android:label="fragment_first"
tools:layout="#layout/fragment_first" />
<fragment
android:id="#+id/secondFragment"
android:name="com.viz.lightweatherforecast.SecondFragment"
android:label="fragment_second"
tools:layout="#layout/fragment_second" />
<fragment
android:id="#+id/thirdFragment"
android:name="com.viz.lightweatherforecast.ThirdFragment"
android:label="fragment_third"
tools:layout="#layout/fragment_third" />
</navigation>
activity_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout"
android:background="#drawable/dubai"
tools:context=".Activity.HomeActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_menu" />
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="409dp"
android:layout_height="599dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:navGraph="#navigation/my_nav"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
HomeActivity:
public class HomeActivity extends AppCompatActivity {
// Last update time, click sound, search button, search panel.
TextView time_field;
MediaPlayer player;
ImageView Search;
EditText textfield;
// For scheduling background image change(using constraint layout, start counting from dubai, down to statue of liberty.
ConstraintLayout constraintLayout;
public static int count=0;
int[] drawable =new int[]{R.drawable.dubai,R.drawable.central_bank_of_nigeria,R.drawable.eiffel_tower,R.drawable.hong_kong,R.drawable.statue_of_liberty};
Timer _t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// use home activity layout.
time_field = findViewById(R.id.textView9);
Search = findViewById(R.id.imageView4);
textfield = findViewById(R.id.textfield);
// find the id's of specific variables.
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
// host 3 fragments along with bottom navigation.
final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
assert navHostFragment != null;
final NavController navController = navHostFragment.getNavController();
NavigationUI.setupWithNavController(bottomNavigationView, navController);
// For scheduling background image change
constraintLayout = findViewById(R.id.layout);
constraintLayout.setBackgroundResource(R.drawable.dubai);
_t = new Timer();
_t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// run on ui thread
runOnUiThread(() -> {
if (count < drawable.length) {
constraintLayout.setBackgroundResource(drawable[count]);
count = (count + 1) % drawable.length;
}
});
}
}, 5000, 5000);
Search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// make click sound when search button is clicked.
player = MediaPlayer.create(HomeActivity.this, R.raw.click);
player.start();
getWeatherData(textfield.getText().toString().trim());
// make use of some fragment's data
FirstFragment firstFragment = (FirstFragment) navHostFragment.getChildFragmentManager().getFragments().get(0);
firstFragment.getWeatherData(textfield.getText().toString().trim());
}
});
}
});
}
}
EDIT
Fragment class:
public class FirstFragment extends Fragment {
// User current time, current temperature, current condition, sunrise, sunset, temperature, pressure, humidity, wind_speed, visibility, clouds
TextView current_temp, current_output, rise_time, set_time, temp_out, Press_out, Humid_out, Ws_out, Visi_out, Cloud_out;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public FirstFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment SecondFragment.
*/
// TODO: Rename and change types and number of parameters
public static FirstFragment newInstance(String param1, String param2) {
FirstFragment fragment = new FirstFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
// For displaying weather data
current_temp = rootView.findViewById(R.id.textView10);
current_output = rootView.findViewById(R.id.textView11);
rise_time = rootView.findViewById(R.id.textView25);
set_time = rootView.findViewById(R.id.textView26);
temp_out = rootView.findViewById(R.id.textView28);
Press_out = rootView.findViewById(R.id.textView29);
Humid_out = rootView.findViewById(R.id.textView30);
Ws_out = rootView.findViewById(R.id.textView33);
Visi_out = rootView.findViewById(R.id.textView34);
Cloud_out = rootView.findViewById(R.id.textView35);
return rootView;
}
public void getWeatherData(String name) {
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<Example> call = apiInterface.getWeatherData(name);
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(#NonNull Call<Example> call, #NonNull Response<Example> response) {
try {
assert response.body() != null;
current_temp.setVisibility(View.VISIBLE);
current_temp.setText(response.body().getMain().getTemp() + " ℃");
current_output.setVisibility(View.VISIBLE);
current_output.setText(response.body().getWeather().get(0).getDescription());
rise_time.setVisibility(View.VISIBLE);
rise_time.setText(response.body().getSys().getSunrise() + " ");
set_time.setVisibility(View.VISIBLE);
set_time.setText(response.body().getSys().getSunset() + " ");
temp_out.setVisibility(View.VISIBLE);
temp_out.setText(response.body().getMain().getTemp() + " ℃");
Press_out.setVisibility(View.VISIBLE);
Press_out.setText(response.body().getMain().getPressure() + " hpa");
Humid_out.setVisibility(View.VISIBLE);
Humid_out.setText(response.body().getMain().getHumidity() + " %");
Ws_out.setVisibility(View.VISIBLE);
Ws_out.setText(response.body().getWind().getSpeed() + " Km/h");
Visi_out.setVisibility(View.VISIBLE);
Visi_out.setText(response.body().getVisibility() + " m");
Cloud_out.setVisibility(View.VISIBLE);
Cloud_out.setText(response.body().getClouds().getAll() + " %");
} catch (Exception e) {
Log.e("TAG", "No City found");
current_temp.setVisibility(View.GONE);
current_output.setVisibility(View.GONE);
rise_time.setVisibility(View.GONE);
set_time.setVisibility(View.GONE);
temp_out.setVisibility(View.GONE);
Press_out.setVisibility(View.GONE);
Humid_out.setVisibility(View.GONE);
Ws_out.setVisibility(View.GONE);
Visi_out.setVisibility(View.GONE);
Cloud_out.setVisibility(View.GONE);
Toast.makeText(getActivity(), "No City found", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(#NotNull Call<Example> call, #NotNull Throwable t) {
t.printStackTrace();
}
});
}
}
I didn't paste everything because I'm following https://stackoverflow.com/help/minimal-reproducible-example
Your problem is that your FirstFragment is not saving its state properly. As per the Saving state with fragments guide:
To ensure the user's state is saved, the Android framework automatically saves and restores the fragments and the back stack. Therefore, you need to ensure that any data in your fragment is saved and restored as well.
But you aren't saving the last name that you pass to getWeatherData, nor are you saving your Example object that you get from your API call in order to repopulate your views when they are recreated.
So need to actually use the APIs described in that guide to save your state. Namely, you should file the Guide to app architecture, which explains how you can separate your from your data loading by using ViewModels to store data across configuration changes (like rotating your device) and LiveData to automatically populate your UI whenever your data is loaded.
The first thing we want to do is move the data loading to a ViewModel. This object survives configuration changes which means any data stored in this class is automatically saved when you rotate your device. This is how we can save your Example class and avoid calling the server over and over.
By using the APIs in the Saved State module for ViewModel (specifically, the SavedStateHandle class), any data you save in there will survive your process being killed and later recreated (say, if your device is low on memory, etc.). This is how we can save the last name so that we will automatically requery for your data.
Here, our ViewModel handles all of the loading from the server and uses a LiveData to let our UI automatically update as the data is loaded.
public class WeatherDataViewModel extends ViewModel {
// This will save the city name
private SavedStateHandle state;
// This is where we'll store our result from the server
private MutableLiveData<Example> mutableWeatherData = new MutableLiveData<>();
public WeatherDataViewModel(SavedStateHandle savedStateHandle) {
state = savedStateHandle;
String savedCityName = state.get("name");
if (savedCityName != null) {
// We already had a previously saved name, so we'll
// start loading right away
loadData();
}
}
// This is what our Fragment will use to get the latest weather data
public LiveData<Example> getWeatherDataLiveData() {
return mutableWeatherData;
}
// When you get a new city name, we'll save that in our
// state, then load the new data from the server
public void setCityName(String name) {
state.set("name", name);
loadData();
}
private void loadData() {
// Get the last name that was set
String name = state.get("name");
// Now kick off a load from the server
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<Example> call = apiInterface.getWeatherData(name);
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(#NonNull Call<Example> call, #NonNull Response<Example> response) {
// Save the response we've gotten
// This will automatically update our UI
mutableWeatherData.setValue(response.body());
}
#Override
public void onFailure(#NotNull Call<Example> call, #NotNull Throwable t) {
t.printStackTrace();
}
});
}
}
Now you can rewrite your FirstFragment to use the WeatherDataViewModel as the source of truth for your UI:
public class FirstFragment extends Fragment {
private WeatherDataViewModel viewModel;
public FirstFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
// For displaying weather data
final TextView current_temp = rootView.findViewById(R.id.textView10);
final TextView current_output = rootView.findViewById(R.id.textView11);
final TextView rise_time = rootView.findViewById(R.id.textView25);
final TextView set_time = rootView.findViewById(R.id.textView26);
final TextView temp_out = rootView.findViewById(R.id.textView28);
final TextView Press_out = rootView.findViewById(R.id.textView29);
final TextView Humid_out = rootView.findViewById(R.id.textView30);
final TextView Ws_out = rootView.findViewById(R.id.textView33);
final TextView Visi_out = rootView.findViewById(R.id.textView34);
final TextView Cloud_out = rootView.findViewById(R.id.textView35);
// Get our ViewModel instance
viewModel = new ViewModelProvider(this).get(WeatherDataViewModel.class);
// And whenever the data changes, refresh the UI
viewModel.getWeatherDataLiveData().observe(getViewLifecycleOwner(), data -> {
if (data != null) {
current_temp.setVisibility(View.VISIBLE);
current_temp.setText(data.getMain().getTemp() + " ℃");
current_output.setVisibility(View.VISIBLE);
current_output.setText(data.getWeather().get(0).getDescription());
rise_time.setVisibility(View.VISIBLE);
rise_time.setText(data.getSys().getSunrise() + " ");
set_time.setVisibility(View.VISIBLE);
set_time.setText(data.getSys().getSunset() + " ");
temp_out.setVisibility(View.VISIBLE);
temp_out.setText(data.getMain().getTemp() + " ℃");
Press_out.setVisibility(View.VISIBLE);
Press_out.setText(data.getMain().getPressure() + " hpa");
Humid_out.setVisibility(View.VISIBLE);
Humid_out.setText(data.getMain().getHumidity() + " %");
Ws_out.setVisibility(View.VISIBLE);
Ws_out.setText(data.getWind().getSpeed() + " Km/h");
Visi_out.setVisibility(View.VISIBLE);
Visi_out.setText(data.getVisibility() + " m");
Cloud_out.setVisibility(View.VISIBLE);
Cloud_out.setText(data.getClouds().getAll() + " %");
} else {
Log.e("TAG", "No City found");
current_temp.setVisibility(View.GONE);
current_output.setVisibility(View.GONE);
rise_time.setVisibility(View.GONE);
set_time.setVisibility(View.GONE);
temp_out.setVisibility(View.GONE);
Press_out.setVisibility(View.GONE);
Humid_out.setVisibility(View.GONE);
Ws_out.setVisibility(View.GONE);
Visi_out.setVisibility(View.GONE);
Cloud_out.setVisibility(View.GONE);
Toast.makeText(requireActivity(), "No City found", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
public void getWeatherData(String name) {
// The ViewModel controls loading the data, so we just
// tell it what the new name is - this kicks off loading
// the data, which will automatically call through to
// our observe() call when the data load completes
viewModel.setCityName(name);
}
}
With these changes, you'll find that your Fragment now correctly handles:
Being put on the Fragment back stack
Configuration changes (i.e., rotating your device)
Process death and recreation (i.e., testing with 'Don't keep activities' on)
You'll note how we use new ViewModelProvider(this).get(WeatherDataViewModel.class) - that creates a WeatherDataViewModel that is tied to this - your Fragment itself. This is best if the data your ViewModel loads is only used in that one Fragment.
If you also wanted to use this same data in your Activity, your activity could use new ViewModelProvider(this).get(WeatherDataViewModel.class) to create a WeatherDataViewModel that is scoped to the entire Activity. Any Fragment could then use new ViewModelProvider(requireActivity()).get(WeatherDataViewModel.class) to get that Activity owned ViewModel. This would potentially mean that you wouldn't need a getWeatherData() method on your Fragment at all - instead, your Activity would directly call viewModel.setCityName(name) itself and all Fragments would just instantly update (as they read from the same ViewModel).
I think, this is because your FirstFragment gets reinstantiated every time. To save state, modify onCreate() of your FirstFragment like following:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mParam1 = savedInstanceState.getString(ARG_PARAM1);
mParam2 = savedInstanceState.getString(ARG_PARAM2);
} else if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
And add onSaveInstanceState() to your FirstFragment:
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(ARG_PARAM1, mParam1);
outState.putString(ARG_PARAM2, mParam2);
}
The mistake seems to be that every time you select an item in the bottom navigation bar the fragment gets re-instantized thus creating a new fragment(of same type, but a new object of that fragment).
I would suggest you create an object of all the fragment at the start like this
private final Fragment[] fragments = {
new HomeFragment(),
new MyNotesFragment(),
new PreferencesFragment()
};
and then for the Bottom Nav Listener part, we will change the fragments according to the button clicked by passing already made instances of fragments rather than creating new ones.
bottom_navigation_view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Checking which bottom_nav items is clicked by comparing with string names..
if (item.getTitle().toString().equals("Home")) {
// Code to change the fragments
final FragmentTransaction ft =
getFragmentManager().beginTransaction();
// we passed the already made instance of the home fragment rather than creating a new one
ft.replace(R.id.my_fragment, fragments[0], "HomeFrag");
ft.commit();
} else if (item.getTitle().toString().equals("My Notes")) {
// Code to change the fragments
final FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.my_fragment, fragments[1], "MyNotesFrag");
ft.commit();
} else if (item.getTitle().toString().equals("Preferences")) {
// Code to change the fragments
final FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.my_fragment, fragments[2], "PrefFrag");
ft.commit();
}
return true;
}
});
(Though this is for the ViewPager library the concept works the same)
If you still have not solve it yet, you might want to try this solution that uses SharedPreferences.
public class FirstFragment extends Fragment
{
// ...
private SharedPreferences prefs;
private SharedPreferences.Editor prefsEditor;
// ...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
// For displaying weather data
current_temp = rootView.findViewById(R.id.textView10);
current_output = rootView.findViewById(R.id.textView11);
// ...
prefs = new SharedPreferences(getActivity().getApplicationContext());
prefsEditor = prefs.edit();
current_temp.setText(prefs.getString("current_temp", "not yet initialised"));
current_output.setText(prefs.getString("current_output", "not yet initialised"));
// ...
return rootView;
}
public void getWeatherData(String name)
{
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<Example> call = apiInterface.getWeatherData(name);
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(#NonNull Call<Example> call, #NonNull Response<Example> response) {
try {
assert response.body() != null;
final String currentTemp = response.body().getMain().getTemp() + " ℃";
editor.putString("current_temp", currentTemp);
editor.commit();
current_temp.setText(currentTemp);
current_temp.setVisibility(View.VISIBLE);
final String currentOutput = response.body().getWeather().get(0).getDescription();
editor.putString("current_output", currentOutput);
editor.commit();
current_output.setText(currentOutput);
current_output.setVisibility(View.VISIBLE);
// ...
} catch (Exception e) {
Log.e("TAG", "No City found");
current_temp.setVisibility(View.GONE);
current_output.setVisibility(View.GONE);
// ...
Toast.makeText(getActivity(), "No City found", Toast.LENGTH_SHORT).show();
}
}
// ...
});
}
}

Placing "Native Advanced Ads" inside popup close

I try to put advanced ads inside the dialog box when you close the application, but when you open a dialog box does not load the ad for the first time. ... I am worried that I load the ad inside the application
without appearing and at closing I put it in the dialog box for fear that the agent considers it a google violation to download the ad without its appearance
Constant code from android developer
public class MainActivity extends AppCompatActivity {
private static final String ADMOB_AD_UNIT_ID = "ca-app-pub-3940256099942544/2247696110";
private static final String ADMOB_APP_ID = "ca-app-pub-3940256099942544~3347511713";
AdLoader.Builder builder;
UnifiedNativeAdView adView;
private Button refresh;
private CheckBox startVideoAdsMuted;
private TextView videoStatus;
private UnifiedNativeAd nativeAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, ADMOB_APP_ID);
refresh = findViewById(R.id.btn_refresh);
startVideoAdsMuted = findViewById(R.id.cb_start_muted);
videoStatus = findViewById(R.id.tv_video_status);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View unusedView) {
refreshAd();
}
});
refreshAd();
}
/**
* Populates a {#link UnifiedNativeAdView} object with data from a given
* {#link UnifiedNativeAd}.
*
* #param nativeAd the object containing the ad's assets
* #param adView the view to be populated
*/
private void populateUnifiedNativeAdView(UnifiedNativeAd nativeAd, UnifiedNativeAdView adView) {
// Set the media view. Media content will be automatically populated in the media view once
// adView.setNativeAd() is called.
MediaView mediaView = adView.findViewById(R.id.ad_media);
adView.setMediaView(mediaView);
// Set other ad assets.
adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
adView.setBodyView(adView.findViewById(R.id.ad_body));
adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
adView.setIconView(adView.findViewById(R.id.ad_app_icon));
adView.setPriceView(adView.findViewById(R.id.ad_price));
adView.setStarRatingView(adView.findViewById(R.id.ad_stars));
adView.setStoreView(adView.findViewById(R.id.ad_store));
adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
// The headline is guaranteed to be in every UnifiedNativeAd.
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
// These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
// check before trying to display them.
if (nativeAd.getBody() == null) {
adView.getBodyView().setVisibility(View.INVISIBLE);
} else {
adView.getBodyView().setVisibility(View.VISIBLE);
((TextView) adView.getBodyView()).setText(nativeAd.getBody());
}
if (nativeAd.getCallToAction() == null) {
adView.getCallToActionView().setVisibility(View.INVISIBLE);
} else {
adView.getCallToActionView().setVisibility(View.VISIBLE);
((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
}
if (nativeAd.getIcon() == null) {
adView.getIconView().setVisibility(View.GONE);
} else {
((ImageView) adView.getIconView()).setImageDrawable(
nativeAd.getIcon().getDrawable());
adView.getIconView().setVisibility(View.VISIBLE);
}
if (nativeAd.getPrice() == null) {
adView.getPriceView().setVisibility(View.INVISIBLE);
} else {
adView.getPriceView().setVisibility(View.VISIBLE);
((TextView) adView.getPriceView()).setText(nativeAd.getPrice());
}
if (nativeAd.getStore() == null) {
adView.getStoreView().setVisibility(View.INVISIBLE);
} else {
adView.getStoreView().setVisibility(View.VISIBLE);
((TextView) adView.getStoreView()).setText(nativeAd.getStore());
}
if (nativeAd.getStarRating() == null) {
adView.getStarRatingView().setVisibility(View.INVISIBLE);
} else {
((RatingBar) adView.getStarRatingView())
.setRating(nativeAd.getStarRating().floatValue());
adView.getStarRatingView().setVisibility(View.VISIBLE);
}
if (nativeAd.getAdvertiser() == null) {
adView.getAdvertiserView().setVisibility(View.INVISIBLE);
} else {
((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
adView.getAdvertiserView().setVisibility(View.VISIBLE);
}
// This method tells the Google Mobile Ads SDK that you have finished populating your
// native ad view with this native ad. The SDK will populate the adView's MediaView
// with the media content from this native ad.
adView.setNativeAd(nativeAd);
// Get the video controller for the ad. One will always be provided, even if the ad doesn't
// have a video asset.
VideoController vc = nativeAd.getVideoController();
// Updates the UI to say whether or not this ad has a video asset.
if (vc.hasVideoContent()) {
videoStatus.setText(String.format(Locale.getDefault(),
"Video status: Ad contains a %.2f:1 video asset.",
vc.getAspectRatio()));
// Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The
// VideoController will call methods on this object when events occur in the video
// lifecycle.
vc.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
#Override
public void onVideoEnd() {
// Publishers should allow native ads to complete video playback before
// refreshing or replacing them with another ad in the same UI location.
refresh.setEnabled(true);
videoStatus.setText("Video status: Video playback has ended.");
super.onVideoEnd();
}
});
} else {
videoStatus.setText("Video status: Ad does not contain a video asset.");
refresh.setEnabled(true);
}
}
/**
* Creates a request for a new native ad based on the boolean parameters and calls the
* corresponding "populate" method when one is successfully returned.
*
*/
private void refreshAd() {
refresh.setEnabled(false);
builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID);
builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
// OnUnifiedNativeAdLoadedListener implementation.
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
if (nativeAd != null) {
nativeAd.destroy();
}
nativeAd = unifiedNativeAd;
FrameLayout frameLayout =
findViewById(R.id.fl_adplaceholder);
adView = (UnifiedNativeAdView) getLayoutInflater()
.inflate(R.layout.ad_unified, null);
populateUnifiedNativeAdView(unifiedNativeAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
VideoOptions videoOptions = new VideoOptions.Builder()
.setStartMuted(startVideoAdsMuted.isChecked())
.build();
NativeAdOptions adOptions = new NativeAdOptions.Builder()
.setVideoOptions(videoOptions)
.build();
builder.withNativeAdOptions(adOptions);
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
refresh.setEnabled(true);
Toast.makeText(MainActivity.this, "Failed to load native ad: "
+ errorCode, Toast.LENGTH_SHORT).show();
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
videoStatus.setText("");
}
Now I'm trying to put the code refresh method insaid dialog box instead of refresh method
public void showdilog(){
builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID);
builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
// OnUnifiedNativeAdLoadedListener implementation.
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
if (nativeAd != null) {
nativeAd.destroy();
}
nativeAd = unifiedNativeAd;
FrameLayout frameLayout =
findViewById(R.id.fl_adplaceholder);
adView = (UnifiedNativeAdView) getLayoutInflater()
.inflate(R.layout.ad_unified, null);
populateUnifiedNativeAdView(unifiedNativeAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
VideoOptions videoOptions = new VideoOptions.Builder()
.setStartMuted(startVideoAdsMuted.isChecked())
.build();
NativeAdOptions adOptions = new NativeAdOptions.Builder()
.setVideoOptions(videoOptions)
.build();
builder.withNativeAdOptions(adOptions);
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
refresh.setEnabled(true);
Toast.makeText(MainActivity.this, "Failed to load native ad: "
+ errorCode, Toast.LENGTH_SHORT).show();
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
bulider.setView(adView);
builder.setMessage(R.string.onfirm_exit)
.setCancelable(false)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
What you're doing is that you're building and showing your dialog box at the same time. So, at the time you show your dialog box you're also loading your native ad.
You should BUILD your dialog box in advance, and don't call alert.show() in your build function.
So, call your buildDialog() in MainActivity in advance.
In your onBackPressed function call alert.show().
Hope it helps.

AdView Not Showing on first load

Ive been working on learning how to make games and Id like to understand how to add Adverts. The advert shows? But only after I close and reopen, "SMART_BANNER" doesnt work either. What am I doing wrong?
public class MainActivity extends FragmentActivity {
public GoogleApiClient apiClient;
private MainActivity main = this;
public GameSurface gameSurface;
RelativeLayout layout;
RelativeLayout adlayout;
private Saver saver;
private static final String HIGHSCORE = "highscore";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
apiClient = new GoogleApiClient.Builder(this)
.addApi(Games.API)
.addScope(Games.SCOPE_GAMES)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Toast.makeText(getBaseContext(), "Connection To Google Games Failed, No App Found Or No Internet", Toast.LENGTH_SHORT).show();
}
}).build();
MobileAds.initialize(this, getString(R.string.adappid));
apiClient.connect();
saver = Saver.getInstance(this);
playerscores();
// fullscreen
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
layout = new RelativeLayout(this);
layout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
adView.loadAd(adRequestBuilder.build());
//ads
gameSurface = new GameSurface(this, main);
layout.addView(gameSurface);
layout.addView(adView);
setContentView(layout);
}
// Set No Title
//this.setContentView(new GameSurface(this));
public void playerscores() {
if (apiClient != null && apiClient.isConnected()) {
Games.Leaderboards.loadCurrentPlayerLeaderboardScore(apiClient, "CgkI08DA0-sZEAIQAQ", LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(
new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
#Override
public void onResult(Leaderboards.LoadPlayerScoreResult arg0) {
LeaderboardScore c = arg0.getScore();
String score = c.getDisplayScore();
saver.saveString(HIGHSCORE, score);
}
});
}
}
public void gameover() {
if (apiClient != null && apiClient.isConnected()) {
Games.Leaderboards.submitScore(apiClient, getString(R.string.leaderboard_highscores), GameSurface.HighScore);
}
}
public void showLeaderboard() {
if (apiClient != null && apiClient.isConnected()) {
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(apiClient, "CgkI08DA0-sZEAIQAQ"), 1);
} else {
apiClient.connect();
}
}
}
Basically I want the advert to show as soon as the app opens. Id Also like it to be fixed to the bottom, I cant find a way to do this, I've tried adding gravity but adview doesn't have this attribute.
Any advice as to what Im doing wrong will be greatly appreciated.
Part Answer I worked out how to get the advert align to the bottom and Center it.
RelativeLayout.LayoutParams viewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);viewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);viewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
adView.setLayoutParams(viewParams);
EDIT [SOLVED]
added an adview listener, reading through admob docs and seeing various listeners gave me an idea!
then just made it redraw its self.
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
adView.setVisibility(View.GONE);
adView.setVisibility(View.VISIBLE);
}
});
you should add the view before loading the ad, if you want to show the ad faster just call setContentView() as soon as possible and then load the ad

Have an OnBackPressed() alert box to appear only for 2 out of the 3 fragments

I am looking for a way to have an "Are you sure you have finished?" alert box to come up when the user presses the back navigation button whilst on the edit note and add note fragment but not whilst they're on the view note fragment. Below I have attached code from the activity which each fragment is linked to. If you need more of my code from different areas, please let me know.
public class NoteDetailActivity extends AppCompatActivity {
public static final String NEW_NOTE_EXTRA = "New Note";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_detail);
createAndFragment();
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
}
private void createAndFragment(){
Intent intent = getIntent();
MainActivity.FragmentToLaunch fragmentToLaunch = (MainActivity.FragmentToLaunch) intent.getSerializableExtra(MainActivity.NOTE_FRAGMENT_TO_LOAD_EXTRA);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch(fragmentToLaunch){
case EDIT:
NoteEditFragment noteEditFragment = new NoteEditFragment();
setTitle(R.string.edit_fragment_title);
fragmentTransaction.add(R.id.note_container, noteEditFragment, "NOTE_EDIT_FRAGMENT");
break;
case VIEW:
NoteViewFragment noteViewFragment = new NoteViewFragment();
setTitle(R.string.view_fragment_title);
fragmentTransaction.add(R.id.note_container, noteViewFragment, "NOTE_VIEW_FRAGMENT");
break;
case CREATE:
NoteEditFragment noteCreateFragment = new NoteEditFragment();
setTitle(R.string.create_fragment_title);
Bundle bundle = new Bundle();
bundle.putBoolean(NEW_NOTE_EXTRA, true);
noteCreateFragment.setArguments(bundle);
fragmentTransaction.add(R.id.note_container, noteCreateFragment, "NOTE_CREATE_FRAGMENT");
break;
}
fragmentTransaction.commit();
}
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
NoteDetailActivity.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
Do this by getting the fragment tag
get tag like this
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_container);
String tag = fragment.getTag(); // This will return the tag of fragment
Then use if-else to do the task
if (tag.equalsIgnoreCase("Home")) {
// Open aLERT box
}else if (tag.equalsIgnoreCase("Home2")) {
// Open aLERT box
}else if (tag.equalsIgnoreCase("Home2")) {
// Open aLERT box
}else{
// dO STUFF HERE
}
Don't forget to add the tag of fragment
First of all, try refraining from taking static tags as the names of fragment. Instead,you can set fragment's name as ABCFragment.class.getSimpleName().
Now for the checks on dialogs, create a method
/**
* This method is used to get the top fragmnet on the stack
*
* #return {#link Fragment}
*/
protected final Fragment getTopFragmentStack() {
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = null;
for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
fragment = fm.findFragmentByTag(fm.getBackStackEntryAt(entry)
.getName());
}
return fragment;
}
After this to check which fragment is visible at the moment, you can use the following code
Fragment topRunningFragment = getTopFragmentStack();
if (topRunningFragment instanceof ABCFragment) {
showDialog();
} else if (topRunningFragment instanceof ABCDFragment) {
showDialog();
} else if (topRunningFragment instanceof ABCDEFragment) {
// Do nothing
}

What is the proper way to handle orientation change when a custom alertdialog has an open spinner?

In my app I have a custom AlertDialog (handled by the system using showDialog()) which contains a tabhost with 2 tabs. In one of the tabs is a spinner. I can rotate my screen without problems as long as the spinner isn't open (spinner dialog displayed). If the spinner is open during rotation, I get this:
FATAL EXCEPTION: main
java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
at android.view.Window$LocalWindowManager.removeView(Window.java:432)
at android.app.Dialog.dismissDialog(Dialog.java:278)
at android.app.Dialog.access$000(Dialog.java:71)
at android.app.Dialog$1.run(Dialog.java:111)
at android.app.Dialog.dismiss(Dialog.java:268)
at android.widget.Spinner.onDetachedFromWindow(Spinner.java:89)
at android.view.View.dispatchDetachedFromWindow(View.java:6173)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1164)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162)
at android.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1746)
at android.view.ViewRoot.doDie(ViewRoot.java:2757)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1995)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
So...
1 - Is it possible to programmatically close the spinner during onPause()?
2 - Is there a different method I should be using?
3 - If there isn't a proper solution, how do I catch this particular Exception? (Bad practice I know, but the crashing needs to be stopped, and since the activity reconstructs itself properly after being destroyed, it wouldn't matter anyway.
... And please for the love of all that is holy, do not suggest that I add android:configChanges="orientation" to my activity declaration. It amazes me how often that is accepted as the official fix-all for orientation issues.
Edit for additional info
Here is my dialog creation code for reference:
static final int ID_DIALOG_CHOOSER = 1;
int pref_location;
private Dialog dialog;
...
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case ID_DIALOG_CHOOSER:
dialog = show(ID_DIALOG_CHOOSER);
break;
}
return dialog;
}
...
showDialog(DialogView.ID_DIALOG_CHOOSER);
...
Dialog show(final int dialogType)
{
if (dialogType == ID_DIALOG_CHOOSER)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// inflate tabhost layout
View tabHostLayout = (View) inflater.inflate(R.layout.tabhost_layout, null);
FrameLayout tabContent = (FrameLayout) tabHostLayout.findViewById(android.R.id.tabcontent);
// inflate tab content layouts and add to tabhost layout
LinearLayout tab1 = (LinearLayout) inflater.inflate(R.layout.dialog_tab1, null);
tabContent.addView(tab1);
LinearLayout tab2 = (LinearLayout) inflater.inflate(R.layout.dialog_tab2, null);
tabContent.addView(tab2);
// tab setup
TabHost tabHost = (TabHost) tabHostLayout.findViewById(R.id.TabHost_Dialog_Tabs);
tabHost.setup();
TabHost.TabSpec tab_1 = tabHost.newTabSpec("Category 1");
tab_1.setContent(R.id.LinearLayout_Dialog_Tab1_Content);
tab_1.setIndicator(this.getResources().getString(R.string.dialog_tab1), this.getResources().getDrawable(R.drawable.tabhost_icon_selector));
tabHost.addTab(tab_1);
TabHost.TabSpec tab_2 = tabHost.newTabSpec("Category 2");
tab_2.setContent(R.id.LinearLayout_Dialog_Tab2_Content);
tab_2.setIndicator(this.getResources().getString(R.string.dialog_tab2), this.getResources().getDrawable(R.drawable.tabhost_icon_selector));
tabHost.addTab(tab_2);
// spinner setup
final Spinner spinner_location = (Spinner) tab1.findViewById(R.id.Spinner_Dialog_Location);
String[] locationArrayVals = null;
ArrayAdapter<CharSequence> adapter_location = null;
locationArrayVals = this.getResources().getStringArray(R.array.location_array_vals);
adapter_location = ArrayAdapter.createFromResource(this, R.array.location_array_listdisplay, android.R.layout.simple_spinner_item);
adapter_location.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_location.setAdapter(adapter_location);
// ok button
Button button_ok = (Button) tab1.findViewById(R.id.Button_Dialog_OK);
button_ok.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
pref_location = spinner_location.getSelectedItemPosition();
dialog.dismiss();
}
});
// create dialog
builder.setTitle("Location")
.setView(tabHost)
.setCancelable(true);
dialog = builder.create();
}
return dialog;
}
Edit with temporary workaround
For anybody interested I managed to at least stop the crashing by subclassing spinner and overriding onDetachedFromWindow() like so:
public static class CatchingSpinner extends Spinner
{
public CatchingSpinner(Context context, AttributeSet attrs)
{
super(context, attrs);
}
#Override
protected void onDetachedFromWindow()
{
try
{
super.onDetachedFromWindow();
}
catch (IllegalArgumentException e)
{
// do whatever
}
}
}
Like I said, a workaround. Still working on a solution. :/
I found a better solution. It is actually not so hard to replace Spinner with a button that looks and behaves like Spinner (except the crash, fortunately).
<Button
android:background="#android:drawable/btn_dropdown"
android:gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
public void showSpinner() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(_spinnerPrompt);
builder.setSingleChoiceItems(_spinnerOptions, _spinnerSelection,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
_spinnerSelection = item;
_pseudoSpinner.setText(_spinnerOptions[item]);
_restoreSpinnerOnRestart = false;
dialog.dismiss();
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
_restoreSpinnerOnRestart = false;
}
});
AlertDialog alert = builder.create();
_restoreSpinnerOnRestart = true;
alert.show();
}
#Override
public Bundle onSaveInstanceState() {
Bundle state = super.onSaveInstanceState();
state.putBoolean(STATE_SPINNER_RESTORE, _restoreSpinnerOnRestart);
state.putInt(STATE_SPINNER_SELECTION, _spinnerSelection);
return state;
};
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
_spinnerSelection = savedInstanceState.getInt(STATE_SPINNER_SELECTION, -1);
_restoreSpinnerOnRestart = savedInstanceState.getBoolean(STATE_SPINNER_RESTORE);
_pseudoSpinner.setText(_spinnerOptions[_spinnerSelection]);
if (_restoreSpinnerOnRestart) {
showSpinner();
}
};
Ok, I think I've found your problem: dialog.dismiss();
You are doing really strange thing - you are creating dialog through activity but say dismiss to it not to activity to dismiss it - those are two different approaches which you can't mix. You should choose one method: dialog.show and dialog.dismiss or activity.showDialog() and activity.dismissDialog() <- this is better because handles orientation change.
What you should do is just remove dialog from class variables, use activity.showDialog everywhere. Probably your problem was in dismissing dialog in onClick. Just use YourActivityName.this.dismissDialog(DIALOG_NUMBER) and it should work.
Read more about dialogs form developers site - I had also such problems and taken long time to learn how it works but after all - dialogs are not so complicated ;)
I had a similar a crash. I worked around by disabling orientation changes when dialog is displayed.
#Override
public void onDismiss(DialogInterface dialog) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
#Override
public void onShow(DialogInterface dialog) {
int loadedOrientation = getResources().getConfiguration().orientation;
int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
if (loadedOrientation == Configuration.ORIENTATION_LANDSCAPE) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else if (loadedOrientation == Configuration.ORIENTATION_PORTRAIT) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
setRequestedOrientation(requestedOrientation);
}
NOTE: I actually found that there is no reliable way to lock screen orientation.

Categories

Resources