Reputation: 15
I am developing a simple app which has a few activities. So when I start the app and get to my MainActivity I have my action bar and everything is great. There are 2 buttons in that MainActivity which when clicked take you to other activities. What I want to do is I want to implement an action bar to those secondary activities. I have tried everything I could find on google or youtube and nothing worked. Hope you guys can help me. I wish to put on that action bar the BACK button which will take me back to activity_main but of course I need an action bar first. I will give you the code for one of my secondary activities so that you could advise me what to add.
Newton Activity
public class NewtonScreen extends Activity
{
private Button theAnswerButton, theHintButton, theSuckButton;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.newton_layout);
theAnswerButton = (Button) findViewById(R.id.answer_button);
theHintButton = (Button) findViewById(R.id.hint_button);
theSuckButton = (Button)findViewById(R.id.suck_at_physics_button);
}
/**@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.newton_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}**/
// Some other methods that are not important for this issue
As you can see I have tried to put these 2 Override methods and I have even created a newton_menu layout but it seems it does not work.
Newton menu
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".NewtonScreen">
<item
android:id="@+id/newton_action_exit"
android:title="@string/action_exit"
android:orderInCategory="101"
app:showAsAction="ifRoom"
/>
Upvotes: 1
Views: 2804
Reputation: 171
Just extend all your activities with Mainactivity(which can be considered as base activity having the actionbar).
eg:Let your MainActivity extends AppCompatActivity
and let other Activities extend with MainActivity like
SecondActivity extends MainActivity.
Hope this will solve your problem.
Upvotes: 0
Reputation: 230
I think your project or your second class not support actionbar.You should change it in style.xml file and make sure second class apply it. Example style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
Upvotes: 1
Reputation: 2922
change extends Activity to extend AppCompatActivity
call this code in oncreate
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and this code
@Override
public boolean onOptionsItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
if(itemId == android.R.id.home){
finish();
}
return true;
}
Upvotes: 1