brian.chisholm.test.helloworldmap.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for brian.chisholm.test.helloworldmap.MainActivity.java

Source

package brian.chisholm.test.helloworldmap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.common.GooglePlayServicesUtil;

import brian.chisholm.test.helloworldmap.model.OfficeLocation;

/**
 * Created by bchisholm on 8/13/2015.
 */
public class MainActivity extends ActionBarActivity {

    public Location mLastLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setTitle(getString(R.string.office_locations));
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        MainActivityFragment fragment = new MainActivityFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        super.onCreateOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
        case android.R.id.home:
            onBackPressed();
            break;
        case R.id.action_legal:
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

            alertDialogBuilder
                    .setMessage(GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(MainActivity.this))
                    .setCancelable(false)
                    .setNeutralButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

            AlertDialog alertDialog = alertDialogBuilder.create();

            alertDialog.show();
            break;
        }
        return super.onOptionsItemSelected(item);

    }

    public void getLocationDetails(OfficeLocation officeLocation) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(officeLocation.getName());
        LocationDetailsFragment ldf = new LocationDetailsFragment();
        Bundle args = new Bundle();
        args.putSerializable("location", officeLocation);
        ldf.setArguments(args);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out, R.anim.pop_slide_in,
                R.anim.pop_slide_out);
        ;
        transaction.replace(R.id.container, ldf);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public void onBackPressed() {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            while (getSupportFragmentManager().getBackStackEntryCount() > 1) {
                super.onBackPressed();
            }
        }
        getSupportActionBar().setTitle(getResources().getString(R.string.office_locations));
        super.onBackPressed();

    }

}