Sunshine app: Launch Setting Activity for SunShine app

github: S06.01-LaunchSettingsActivity S06.02-SettingsFragment , 

S06.03-PolishingPreferences

Settings for Sunshine:

  1. Unit Setting-
    • Fahrenheit  
    • Celsius
  2. Location Setting 

* Reference: Udacity Nanodegree Program- Become an Android Developer *

IMG_20170906_165941.jpg

Screen Shot 2018-03-08 at 12.42.11 PM

public class SettingsActivity extends AppCompatActivity {
    // TODO (1) Add new Activity called SettingsActivity using Android Studio wizard
<!--TODO (2) Add a Settings option to the main menu-->
<item
    android:id= "@+id/action_settings"
    android:orderInCategory="2"
    android:title="@string/action_setting"
    app:showAsAction="never"/>
    // TODO (3) Launch SettingsActivity when the Settings option is clicked

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.action_settings){
            Intent startSettingsActivity = new Intent(this, SettingsActivity.class);
            startActivity(startSettingsActivity);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.activity_settings);

    // TODO (4.1) Set setDisplayHomeAsUpEnabled to true on the support ActionBar
    this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

// TODO (4.2) Add Up Button on SettingsActivity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id == android.R.id.home){
        onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}
<!-- TODO (5) Change MainActivity's launch mode to singleTop -->
<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleTop">

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s