0

The onBackPressed() method is called without me having to write onBackPressed(); inside the onCreate() method. I know the method works because when I comment it out, its effect is removed (which is to exit the program from the MainActivity even if other activities are called).

Also, if I input onBackPressed(); into the MainActivity, then it will not work. I'm really confused why onBackPressed() method works without me having to call the method. Sorry if I did not phrase the question correctly, I hope I did! And thanks in advance!!

public class MainActivity extends ActionBarActivity{

private Button button;
private Button button2;
private ImageView im;
private ImageView im2;
private boolean doubleBackToExitPressedOnce = false;

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

 }
@Override
public void onBackPressed(){
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

public void changeColor() {
    button = (Button) findViewById(R.id.button);
    im = (ImageView) findViewById(R.id.imageView2);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            im.setBackgroundColor(Color.rgb(255,255,255));
        }
    });
  }
 }
Danny Buonocore
  • 3,731
  • 3
  • 24
  • 46
  • onBackPressed is called when the user presses the back key. This call is made by the framework. `I know the method works because when I comment it out, its effect is removed (which is to exit the program from the MainActivity even if other activities are called). Also, if I input onBackPressed(); into the MainActivity, then it will not work` is totally unclear. – Kishore Aug 12 '16 at 23:29
  • All the answers provided make perfect sense. Thank you guys!!! – Aren Tahmasian Aug 13 '16 at 21:08

3 Answers3

2

See that little @Override annotation above the method declaration? That's a handy way to keep track of methods that are inherited from the superclass. That means that MainActivity's superclass, Activity, also has a method called OnBackPressed().

Now, according to Object Oriented Programming design, behaviors and attributes that are common to all activities, are implemented (or at least defined) in the Activity class. Since you can press the back button when in any activity, that method is abstracted to the Activity class. The Android operating system handles calling this method for you, so you don't have to do it yourself every time the back button is pressed. Isn't that nice? So essentially, you can have two classes inherit onBackPressed() from Activity, but have them execute different code. This is known as polymorphism.

More information on inheritance here.

Danny Buonocore
  • 3,731
  • 3
  • 24
  • 46
1

The onBackPressed() method is called without me having to write onBackPressed(); inside the onCreate() method.

First part...

From the docs

Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.

So this is the simple version of "why" it still runs when you don't implement it yourself.

Second part... I'm not sure why you would ever call it in onCreate()

Also, if I input onBackPressed(); into the MainActivity, then it will not work.

It will work how you program it to work.

You have...

@Override
public void onBackPressed(){
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

so you aren't calling it's super method which is why it isn't closing as you would expect.

To fix this, call the method

@Override
public void onBackPressed(){
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    super.onBackPressed(); // add this here

}

Some further Java reading

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

A simple and straight forward answer:

Read what this line says

public class MainActivity extends ActionBarActivity{

You main activity extends ActionBarActivity.

Whenever a class extends another class, it inherits everything from the parent class (in this case ActionBarActivity is your parent class).

If you press control and click on the ActionBarActivity, it will take you into that class and you can see the methods implemented inside it. If you keep going up, you will find that onBackPressed() is implemented somewhere up the hierarchy.

So by default, when you extend an activity, the Activity class is implementing the onBackPressed(). You can change the behaviour of this function by overriding it like:

@Override 
OnBackPressed() {...}

To learn more about inheritance and overriding I suggest you give this a read:

https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Shayan C
  • 1,510
  • 1
  • 13
  • 21