質問

I am using actionbar compat in my application first time. I want to add another activity to the application. When i press a SETTINGS from actionbar, i like to start the second activity.

How is possible ?

codes for my application as follows

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    switch(item.getItemId()){
        case R.id.action_settings:
            Toast.makeText(getBaseContext(), "You selected Settings", Toast.LENGTH_SHORT).show();
            startActivity( new Intent().setClass(this, SecondActivity.class));

            break;
        }
    return true;
}

}

enter image description here mainactivity image

Secondactivity.java

package com.example.test;

import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondactivity);

    // TODO Auto-generated method stub
}

 }

enter image description here secondactivity image

thanks in advance

役に立ちましたか?

解決

Just use a

Intent intent = new Intent(this, YourActivityToStart.class);
startActivity(intent);

on the position where you pop up a Toast.

他のヒント

Using 'app' instead of 'android' namespace prefix for 'actionLayout' attribute fixed it for me.
So, basically, use

app:actionLayout="@layout/yourLayout"

instead of

android:actionLayout="@layout/yourLayout"

I am assuming you are using app_compat library to get the theming engine.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top