Can we invoke one activity into another activity? - android

I Mean that i want to use one activity into another activity, Like class using create instance of that class. Is it Possible?

Well, I think you should use Intents to call an activity from another activity.
Call this from your Activity:
Intent in = new Intent(getApplicationContext(), NextActivity.class);
startActivity(in);

you can do it only by saying startActivity(), no other go. you can't make an instance of Activity because , an Activity gets created when its onCreate() method gets called, but when you say new MyActivity() its default constructor is called and not its onCreate() method (which Android OS will not accept). so always say startActivity() or startActivityForResult() which are handled by android OS

Write this code from where you want to run activity
Intent intent = new Intent(current_Activity_name.this,New_Activity_name.class);
startActivity(intent);
And add the following code into manifest file
<activity android:name=".New_activity_name" />

Well, since an Activity is a displayable-window, the appropriate concept would be that one Activity can be "launched" from another. This is how you achieve that:
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
CurrentActivity.this.startActivity(i);
This code snippet can launch NewActivity from any point in the CurrentActivity code, for example, an 'OnClickListener'.

Yes, it is possible. This is achieved through Intents.
Intent intent = new Intent(this.getApplication(), TARGET_ACTIVITY_NAME.class);
//To add data use intent.putExtra(NAME,VALUE);
intent.setData(data.getData());
try
{
startActivity(intent); // This ll launch the TARGET_ACTIVITY_NAME
}
catch(Exception e)
{
}
For more information refer this link.
Shash

Related

Activity trigger more than once if using launchMode="singleInstance

I have a activity which i declare as launchMode="singleInstance in Manifest file. and using blow code to starting this activity:
Intent intent = new Intent(this, LockScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
To close this activity i am doing like: finishAffinity();
But some how when i finish this activity i can see another open activity instance of this. Can someone tell me how i can finish all OR only create a single one when i need to start this activity from different places inside my application?

Does the Intents get killed after i pass to another Intent Android

I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();

Android create new instance of current activity

I an have activity called A in my project, there is a button that create new instance of the same activity. For example i want to do something like this:
intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(MainActivity.EXTRA_DATA, data);
startActivity(intent);
If i use this code the app crashes when i push the button.
I have found the solution. The activity use a object of another class called data, this class need to implement Serializable.

Close all activities from the first activity in an Android app

I have some activities in an android application and I want to close them all from the first activity.
Is there a way to do that?
Please try the following method.
this.finish();
this.moveTaskToBack(true);
You may call activity A with Intent with FLAG_ACTIVITY_CLEAR_TOP flag set. This brings activity A on top of activities' stack and finishes activities opened from A.
If activity A isn't root activity in application's task you may try FLAG_ACTIVITY_CLEAR_TASK flag.
Try this
Intent intent = new Intent(getApplicationContext(),FirstActivityClass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
AFAIK, There are no methods to close all the activities in the application, but you can simply move the task containing that activities to background (see moveTaskToBack).
Or you can try to send broadcast and in onReceive - finish the activity that received the broadcast
You can define a static vector of activities allActivities in your main activity. In the constructor of each activity, you add a reference to it into the static vector. In the destructor, you remove it.
Whenever you need to close all of them:
for (Activity a : MainActivity.allActivities) a.finish();
MainActivity.allActivities.clear();
If u have MainActivity>B>C>D>E activities and MainActivity is the launcher and U calls all in sequence without calling finish on anyone like :
Intent jump = new Intent(MainActivity.this, B.class);
//finish();
startActivity(jump);
steps:
1) Define static Vector act = new Vector(); in main activity(MainActivity activity) and add act.add(this);
2) Define default constructor in all activities and add references like:
public B()
{
MainActivity.allActivities.add(B.this);
}
3) Call the above code on every exit button clicking:
for (Activity a : MainActivity.allActivities) a.finish();
MainActivity.allActivities.clear();
I hope this will help...!!!

Difference Between 2 Ways to Start an Activity?

I have seen the following two examples of starting activities in Android:
Example 1
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
Example 2
// Calling activity
NextActivity.show(this)
// In the called activity
static void show(Context context) {
final Intent intent = new Intent(context, NextActivity.class);
context.startActivity(intent);
}
It seems the obvious difference between the two examples is that you attach the logic of how an activity is created to the implementation of the activity. Are there any other key differences? (e.g. is the calling activity told to wait until the called activity finishes in one case, but not in the other, etc.)
I see no difference to your 2 methods, other than the 2 lines of code in your first method just happen to be located in a static method that just happens to be located in the 2nd activity's class.
The actual lines of code that are being executed to start the activity are identical. Thus the behavior of the 2 methods will be identical.
Also, the code could be shortened to
context.startActivity(new Intent (context, NextActivity.class));
Only reason to create an instance of Intent as a field is if you need to set flags or add extras, etc.

Categories

Resources