Launch Setting Activity for SunShine app

github: S06.01-LaunchSettingsActivity

IMG_20170906_165941.jpg

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 )

Google+ photo

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

Connecting to %s