I show various fragments in Activity FrameLayout. The first fragment displayed is a Google map. To display the fragment, I change the FrameLayout to the fragment. Then I show the fragment, and then click on the stack. When a backstack appears, the Google map becomes very slow and slow. If I override onBackPressed, remove the popped fragment from the backstack, and then recreate the Google map fragment, the Google map will respond as expected. Overridding onBackPressed seems to work.
Why is the map slow and laggy?
Here is my code (without the onBackPressed function that I mentioned):
Mainactivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayFragment(new MapFragment());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
try {
int id = item.getItemId();
if(id == R.id.displayBlankFragment) {
displayDialogFragment(new BlankFragment());
return true;
}
} catch(Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
private void displayFragment(Fragment fragment) {
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.frame, fragment);
t.commit();
}
private void displayDialogFragment(Fragment fragment) {
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.frame, fragment);
t.addToBackStack(null);
t.commit();
}
}
activity_main.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"
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">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
Mapfragment
public class MapFragment extends Fragment {
private View view;
public MapFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(view == null) {
view = inflater.inflate(R.layout.fragment_map, container, false);
}
return view;
}
}
fragment_map.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" >
</fragment>
</FrameLayout>
Blankfragment
public class BlankFragment extends Fragment {
public BlankFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
fragment_blank.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"/>
</FrameLayout>