Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How To Start New Activity From an Instance

i'm new in Android developing.
I've to start a new Activity. Commonly, i would write the following code:

Intent i = new Intent(Activity1.this, Activity2.class);
Activity1.this.startActivity(i);

but now i need to start a new activity from an instance of that activity (because i don't want to start a generic activity of that type, i need to call his constructor to define his attributes). Something like this:

Activity2 instance = new Activity2(parameters);
Intent i = new Intent(Activity1.this, instance);
Activity1.this.startActivity(i);

Is it possible?

like image 954
Oneiros Avatar asked Jan 19 '11 16:01

Oneiros


People also ask

How do I create a new instance of activity?

FLAG_ACTIVITY_SINGLE_TOP | Intent. FLAG_ACTIVITY_CLEAR_TOP | Intent. FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(storeIntent); It will call onCreate() only once on first launch of activity, if activity is already running it calls only onNewIntent() instead of create new instance of activity or calling onCreate.

How do I add a new activity to an existing project?

In Android Studio 2, just right click on app and select New > Activity > ... to create desired activity type. Save this answer.

How do I start an activity from another app?

If both application have the same signature (meaning that both APPS are yours and signed with the same key), you can call your other app activity as follows: Intent LaunchIntent = getActivity(). getPackageManager(). getLaunchIntentForPackage(CALC_PACKAGE_NAME); startActivity(LaunchIntent);


1 Answers

I think you're better off to add a bundle to your intent, and read the info out that. You pass your parameters with that bundle.

example:

    Intent myIntent = new Intent(this, BlipImageSender.class);
    Bundle paramets = new Bundle();

 paramets.putString("YOUR_PARAM_IDENT","your_parameter_value");

 myIntent.putExtras(paramets);
 this.startActivity(myIntent);

and in your class:

String your_param_value = getIntent().getExtras().getString("YOUR_PARAM_IDENT");
like image 66
Nanne Avatar answered Oct 28 '22 05:10

Nanne