5

I want to implement the ...

@Override
    public void onBackPressed() {
     }

However, I get an error message saying, "Annotations are not allowed here". I need this method to be implemented here. Is there an alternative?

public class supbreh extends Appbreh

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

 if (myBundle != null) {

        String name =  myBundle.getString("workout");
        ShowDetails(name);
    }
}

 private void ShowAbDetails(String mName) {


    if(mName.equals("abs1")){


        @Override
        public void onBackPressed() {  //"Not Allowed here"

        }

}
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • 2
    you cant write method inside method move this `@Override public void onBackPressed() { //"Not Allowed here" }` out of method – N J May 24 '16 at 05:49
  • Is there a way to assign the back button on my android phone its own local variable? –  May 24 '16 at 05:50
  • What does that even mean? ^ – Tim May 24 '16 at 05:53
  • Illegal usage of the method. If your Appbreh class is extended by Activity, you can override the method with-in class. It's basic OOP concept. – Yasir Tahir May 24 '16 at 05:53
  • could you demonstrate the syntax? @YasirTahir –  May 24 '16 at 05:54
  • Have a look at this: http://stackoverflow.com/a/21841637/5392825 – Yasir Tahir May 24 '16 at 05:57
  • @TimCastelijns Every button created from the UI has an id, right? Well, what would be the "Id" for the Back Button on the android phone –  May 24 '16 at 06:18
  • *Every button created from the UI has an id, right?* No they don't. And the back button is not a UI button – Tim May 24 '16 at 06:28

2 Answers2

1
void onBackPressed ()

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.

In here you can't declare this method inside another method .

Only override it in that one Activity

@Override
public void onBackPressed()
{
    super.onBackPressed();  
}

FYI

    @Override
    public void onBackPressed() {

    Intent intent = new Intent(IndividualAbsWorkout.this, IndividualAbsWorkout.class);
    startActivity(intent);
    }
LarsH
  • 27,481
  • 8
  • 94
  • 152
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    Is it possible to start a new activity with void onBackPressed()? –  May 24 '16 at 05:57
  • as in .. Intent nextScreen = new Intent(IndividualAbsWorkout.this, IndividualAbsWorkout.class); nextScreen.putExtra("abWorkout", "abs2"); startActivity(nextScreen); –  May 24 '16 at 05:58
0

You can override onBackPressed as normal and call the method in ShowAbDetails() method like below.

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

 if (myBundle != null) {
    String name =  myBundle.getString("workout");
    ShowDetails(name);
 }

}

private void ShowAbDetails(String mName) {

    if(mName.equals("abs1")){
      onBackPressed();
    }
}


@Override
public void onBackPressed() {  
   // your logic here
}
Tim
  • 41,901
  • 18
  • 127
  • 145
umuieme
  • 729
  • 6
  • 20