I am using MapView in v2 android google maps in a fragment but confused why we need to override all lifecycle methods? - android-mapview

I am using this link https://stackoverflow.com/a/34626302/5761156 and taking reference from here https://gist.github.com/joshdholtz/4522551 but unable to understand why we need to override lifecycle methods such as on resume and on destroy . I tried not to override them and the maps then do not show? What am I doing wrong?
Specifically I am adding the gist lines which I find confusing
#Override
public void onResume() {
mapView.onResume();
super.onResume();
}
#Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

Related

Step Counter in Android Studio doesn't count steps

I have a problem with an app I'm trying to develop in Android Studio. I'm trying to develop a step counter using the STEP_COUNTER sensor in a Fragment.I decided to show my results in a TextView ('mStepsSinceReboot'), but it keeps being empty. Do you have any idea on how to make it work?
An hypotesis is that I should use a Service, but online I only found implementations that don't require it.
Thanks in advance for your answer. This is the code of my Fragment.
public class StepFragment extends Fragment implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mStepCounter;
private boolean isSensorPresent;
private TextView mStepsSinceReboot;
public StepFragment() {
// Required empty public constructor
}
public static StepFragment newInstance(String param1, String param2) {
StepFragment fragment = new StepFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_step, container, false);
mStepsSinceReboot=v.findViewById(R.id.stepssincereboot);
mSensorManager=(SensorManager) this.getActivity().getSystemService(getContext().SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null){
mStepCounter=mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
isSensorPresent=true;
}
else{
isSensorPresent=false;
}
return v;
}
#Override
public void onSensorChanged(SensorEvent event) {
mStepsSinceReboot.setText(("Steps since reboot: "+String.valueOf(event.values[0])));
Log.d("Passi", String.valueOf(mStepsSinceReboot));
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
public void onResume(){
super.onResume();
if(isSensorPresent){
mSensorManager.registerListener(this,mStepCounter,SensorManager.SENSOR_DELAY_NORMAL);
}
}
#Override
public void onPause(){
super.onPause();
if(isSensorPresent){
mSensorManager.unregisterListener(this);
}
}
#Override
public void onDestroy(){
super.onDestroy();
mSensorManager=null;
mStepCounter=null;
}
}
you added the permission in the manifest file ?
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

Invalid member type plugin causing build failure

Today, I opened my Android Studio and tried to build an APK but without changing anything, I got this error: Invalid type in member Type: com/google/gms/googleservices/GoogleServicesPlugin$PluginType; android studio
and here are some other errors that the compiler throws at me:
I tried deleting the 0.jar file from the respective folder and see if that fixes one of the errors but nothing. Any ideas? I don't get what I should change or what I did wrong since I didn't mess around with anything.
EDIT:
public class DokkanCardsApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
#Override
public void onActivityCreated(Activity activity, Bundle bundle) {
// Fix for the Full Screen Activities bug with Android O
if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
#Override
public void onActivityStarted(Activity activity) {
}
#Override
public void onActivityResumed(Activity activity) {
}
#Override
public void onActivityPaused(Activity activity) {
}
#Override
public void onActivityStopped(Activity activity) {
}
#Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
#Override
public void onActivityDestroyed(Activity activity) {
}
});
}
}
You can change your view from Android to Project from top of the left side of your Android Studio and delete .gradle file maybe it helps you.
And I think you have to enable multidex in your application too.
Do it like this:
android {
defaultConfig {
multiDexEnabled true
}
}
Second, add this to four dependencies:
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
And finally make an application class like this:
public class MyApplication extends Application {
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
And finally don't forget to add your application class into your manifest like this:
<application
android:name=".MyApplication"
UPDATE:
Your application class should be like this:
public class DokkanCardsApplication extends Application {
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
#Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
#Override
public void onActivityCreated(Activity activity, Bundle bundle) {
// Fix for the Full Screen Activities bug with Android O
if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
#Override
public void onActivityStarted(Activity activity) {
}
#Override
public void onActivityResumed(Activity activity) {
}
#Override
public void onActivityPaused(Activity activity) {
}
#Override
public void onActivityStopped(Activity activity) {
}
#Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
#Override
public void onActivityDestroyed(Activity activity) {
}
});
}

Building Google api client within Fragment

I am getting an error while building google api client in a fragment. The error I am getting is;
java.lang.ClassCastException: net.janusjanus.we4x4_v1.upload cannot be cast to com.google.android.gms.common.api.GoogleApiClient$OnConnectionFailedListener
I have implemented GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, and LocationListener
and requesting the build as following;
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) getActivity())
.addApi(LocationServices.API)
.build();
first I had a problem declaring the new GoogleApiClient.Builder where I declared it as ;
mGoogleApiClient = new GoogleApiClient.Builder(this)
underlined as an error, thus I had to try several ways to declare it, with getActivity() and getContext(). but I got the same error ?
Any idea on what I am doing wrong here and the correct way ?
Well simply in my case i had to declare the GoogleApiClient.Builder as following;
mGoogleApiClient = new GoogleApiClient.Builder(UploadPictures.this.getContext())
where UploadPictures is the fragment name.
Hope this could be helpful for someone.
implement GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, and LocationListener
#Override
public void onConnectionFailed(ConnectionResult result) {
}
#Override
public void onConnected(Bundle arg0) {
}
#Override
public void onConnectionSuspended(int arg0) {
}
#Override
public void onLocationChanged(Location location) {
}
And then change code to:
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();

ActionBar.Tablistener deprecated

I'm trying to build a swipe tabs view by using fragment activity and actionbar.tablistener. I have referenced other people tutorial and do it, however, it keep on deprecated and do not successful run it. Then I changed to AppComBatActivity and it do not works, show viewgroup error which I don't know why. THen I changed to ActionBarActivity, it worked finally, but no matter how I edit my tab activity, in java class and xml too, it does not show anything.
I have actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) deprecated as well.
I have attached my code as following...
fragmentmenu.class
public class FragmentMenu extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
ViewPager viewPager;
FragmentPageAdapter ft;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_menu);
viewPager = (ViewPager)findViewById(R.id.pager);
ft = new FragmentPageAdapter(getSupportFragmentManager());
actionBar = getActionBar();
viewPager.setAdapter(ft);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("Menu").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Report").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Setting").setTabListener(this));
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
}
</code>
FragmentPagerAdapter
<code>
public class FragmentPageAdapter extends FragmentPagerAdapter {
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new MyActivity();
case 1:
return new Report();
case 2:
return new Setting();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
</code>
One of the fragment
<code>
public class Report extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.report, container, false);
}
}
}}
Please help!
ActionBar.Tablistener is deprecated. You need to use the TabLayout instead.
Check this tutorial for more infos :
http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

actionbarsherlock sherlockfragment google maps v2 is null

I'm trying to use Actionbarsherlock with a tab bar and in one of those taps i want a map.
I have successfully created an map in a sherlockfragment with google map v2, but I want add markers programmatically to the map.
public class Map_fragment extends SherlockFragment {
static final LatLng Cph = new LatLng(55.702355,12.436523);
private final static String MAP_TAG = "map";
private GoogleMap mMap;
SupportMapFragment mMapFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
SupportMapFragment map = (SupportMapFragment) (getSherlockActivity().getSupportFragmentManager().findFragmentByTag(MAP_TAG));
if(map==null){
Log.d("MAP", "NULL");
}
if(map!=null){
Log.d("MAP", " " + map);
Log.d("GETMAP", " " + map.getMap());
}
//GoogleMap mMap = map.getMap();
//mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Cph, 14));
return inflater.inflate(R.layout.mapv2_layout, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
public void setupMap(){
Log.d("SUCCESS" , "SUCCESS");
}
}
I have tried with many different suggestions from other sites and posts on stack overflow, but the map is always null.
I have tried with onCreate and onAttach after suggestions from others, but with no luck.
I have tried for a long time and Im about to pull my hair out :(
I solved it by manually adding the SherlockMapFragment class to the ActionBarSherlock library, found here http://code.google.com/p/opendatakit/source/browse/src/com/actionbarsherlock/app/SherlockMapFragment.java?spec=svn.tables.35872cc920b5668de433c6fc81efc86c4970bac1&repo=tables&r=0c1b0212e5b8bb5af8ce2b8e9357cd30e3870084
Then with the following lines i finally got "gMap" that wasn't null
SupportMapFragment map;
map = (SupportMapFragment) (getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.map));
gMap = map.getMap();

Resources