-6

Is it possible to call an Activity form inside the Activity class, so I can deliver information over constructor?

something like

public MyClass(){
     startActivity(new Intent(this, this.getClass()));
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Janis
  • 3
  • 1

2 Answers2

0

I'm assuming this is because you want to pass some initialisation data to the activity?

The way to do this is by adding a Bundle to the activity.

    Intent intent = new Intent(YourStartingActivity.this,YourNewActivity.class);
    intent.putExtra("key", "value");
    startActivity(intent);

The in the OnCreate method of your new Activity

Bundle bundle = getIntent().getExtras();
String myString = bundle.getString("key");

Which will get whatever you passed in as your "value" which can be a String, int, float etc....

Ivan Wooll
  • 4,145
  • 3
  • 23
  • 34
0

I think you can't do that. If I understood correctly you want to pass data between two activities, you should always do this via Intent. Take a look on this https://stackoverflow.com/a/2091482/3707606

If you want to pass more complex data you should make the data class implement Parcelable. It may look hard but you can generate your parcelable at http://www.parcelabler.com/ or install this plugin to Android Studio.

Community
  • 1
  • 1
lucas_sales
  • 129
  • 2