0

As the title says, I want to know if it is possible to pass extras to an extended class.

Imagine this situation:

public class MyClass extends Activity {

    @Override
    public void onCreate(Bundle sis) {
        super.onCreate(sis);
        Bundle extras = getIntent().getExtras();
        ....
    }
}

public class MyMainClass extends MyClass{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //Here I would like to pass some extras to MyClass

        super.onCreate(savedInstanceState);
        ...
    }
}   

Is it somehow possible to pass extras to MyClass

Favolas
  • 6,963
  • 29
  • 75
  • 127
  • Yes you can pass extras when you call Intent – Avishek Das Mar 25 '15 at 13:55
  • When you call the Intent for starting that activity you can pass those extras. – lucianohgo Mar 25 '15 at 13:56
  • Sorry but fixed the code. `MyMainClass` extends `MyClass` and not `Activity`. Want to pass extras from `MyMainClass` to my extended `MyClass` – Favolas Mar 25 '15 at 14:03
  • 1
    Hope it will work because the Most parent class extends Activity..Better you can do that by this method http://stackoverflow.com/a/29257787/4693713. And you can get the solution.. – Avishek Das Mar 25 '15 at 14:05

1 Answers1

1

Yes, it can be done using

 Intent i = new Intent(MyMainClass.this, MyClass.class);
 i.putExtra("extra", extra);
 startActivity(i);

and you can get that extra back using

 Bundle extras = getIntent().getExtras();
 String extra = extras.getString("extra");
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • Thanks. Sorry but fixed the code. `MyMainClass` extends `MyClass` and not `Activity`. Want to pass extras from `MyMainClass` to my extended `MyClass` – Favolas Mar 25 '15 at 14:03
  • no problem, the code should still work. Have you tried it ? – Yash Sampat Mar 25 '15 at 14:05