Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Activity extend ActionBarActivity?

I downloaded the latest SDK version and when I created a new Android project the MainActivity that automatically generated extends ActionBarActivity, but I want to extend Activity.

public class MainActivity extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @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) {
        // 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}
like image 340
Marcelo Alarcon Avatar asked Mar 13 '14 22:03

Marcelo Alarcon


People also ask

How to set Action bar in android?

To code the elements of ActionBar, create a new directory in the resource folder of the application project files. Right-click on the res folder and selects New -> Directory. Give the name “menu” to the new directory. Further, create a new Menu Resource File by right click on the menu directory.

Should I use AppCompatActivity?

Unless some third-party library you are using insists upon an ActionBarActivity , you should prefer AppCompatActivity over ActionBarActivity . So, given your minSdkVersion in the 15-16 range: If you want the backported Material Design look, use AppCompatActivity.

How to use getSupportActionBar?

To use the ActionBar utility methods, call the activity's getSupportActionBar() method. This method returns a reference to an appcompat ActionBar object. Once you have that reference, you can call any of the ActionBar methods to adjust the app bar. For example, to hide the app bar, call ActionBar.


6 Answers

ActionBarActivity is for backwards-compatibility. So the Android Actionbar will also work on older devices, see documentation.

Base class for activities that use the support library action bar features.

You can add an ActionBar to your activity when running on API level 7 or higher by extending this class for your activity and setting the activity theme to Theme.AppCompat or a similar theme.

It is a subclass of FragmentsActivity and FragmentsActivity extends Activity. ==> so don't worry all things you could do with normal Activities you can also do with an ActionBarActivity.

like image 136
donfuxx Avatar answered Nov 05 '22 04:11

donfuxx


If you want to extend Activity class, you may check "Empty Activity" during the project creation trajectory.

enter image description here

enter image description here

like image 43
jstn Avatar answered Nov 05 '22 05:11

jstn


Simply Replace ActionBarActivity to Activity Then go to top of the code and replace import android.support.v7.app.ActionBarActivity; to import android.app.Activity;

like image 5
Abdulla Sirajudeen Avatar answered Nov 05 '22 05:11

Abdulla Sirajudeen


My guess is when you created the app, you choose the Action bar setting in eclipse. Please notice ActionBarActivity is an extension of Activity, so all features present in Activity will be present in ActionBarActivity.

like image 3
SeahawksRdaBest Avatar answered Nov 05 '22 03:11

SeahawksRdaBest


You can simply replace the ActionBarActivity with Activity since ActionBarActivity is an extension of Activity. You can refer the class hierarchy here: http://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html

like image 3
Vamsi Tallapudi Avatar answered Nov 05 '22 05:11

Vamsi Tallapudi


Nowadays latest Android SDK ActionbarActivity only. which is from IDE and Google. No Problem with that. ActionBarActivity Facilitate to your application which works on Older API also. Don't worry. That's is not problem.

like image 1
Noorul Avatar answered Nov 05 '22 05:11

Noorul