startActivity(new Intent(this, Class.forName(getPackageManager().getApplicationInfo(getPackageName(), 128).metaData.getString("com.sadaf.javafiles.MAIN_ACTIVITY_CLASS_NAME"))));
This code is to create and add another activity to the top UI level
startActivity()
Can be called from any context/activity, mostly used like
finish()
startActivity(new Intent(this, NewActivity.class));
This is used to close the current activity and begin the next
For a more detailed explanation please refer to :
https://developer.android.com/training/basics/firstapp/starting-activity#BuildIntent
startActivity(new Intent(this, Class.forName(getPackageManager().getApplicationInfo(getPackageName(), 128).metaData.getString("com.sadaf.javafiles.MAIN_ACTIVITY_CLASS_NAME"))));
The intent requires a context and a class as parameter, the context is “this” (the current context from the activity that is currently being displayed)
The class is from Class.forName(String) which requires a String value to get the class name from an activity
It gets that String value from calling :
getPackageManager().getApplicationInfo(getPackageName(), 128).metaData.getString(stringKey)
stringKey is the from the projects AndroidManifest.xml file android:name:
<activity android:name="com.sadaf.javafiles.MAIN_ACTIVITY_CLASS_NAME" >
</activity>
So essentially what that piece of code is doing is getting the .class of your declared “main activity” programatically instead of using the “standard” way of just going MainActivity.class
Related
I got log cat message from startAnotherActivity() method
private void startAnotherActivity() {
Log.i(TAG, "Entered startAnotherActivity()");
Intent intent = new Intent();
intent.setAction(ANOTHER_ACTIVITY);
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
Another activity doesn't start, no other messages in log cat.
How can I resolve this issue?
UPDATE#1:
Sorry, I forgot to mention that AnotherActivity is an Activity in the other application, and therefore ANOTHER_ACTIVITY == 'some.other.app.domain.ANOTHER_ACTIVITY'
Shouldn't Dalvik complain if it cannot find specified activity?
One possible reason may be not declaring other activity in the manifest. You can do this like the following:
<activity android:name="your.package.your.activity">
</activity>
And then you can start the activity by doing the following:
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(intent);
Hope this helps.
Since it's an activity in another application, you may need to set the component (fully qualified package name and fully qualified activity name).
See here: How to start activity in another application?
Or here:
Launch an application from another application on Android
Finally I found out my mistake.
In the project there are two similar messages in two activities, so I thought that runs one, but that was another.
Thank you for your assistance!
I have 2 Activities, each in seperate applications. Activity1 has a button the user can click and it calls the second activity using an intent in its onClick() method:
Intent myIntent = getPackageManager().getLaunchIntentForPackage(com.myProject.Activity2);
startActivityForResult(myIntent, 600);
This correctly launches Activity2 from Activity1, but onActivityResult gets called in Activity1 before onCreate gets called in Activity2, instead of in onBackPressed() where I set up the return intent.
Here is the onCreate method for Activity2:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Here is the current version of onBackPressed method for Activity2:
#Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("Stuff", someStuff);
if(getParent()==null){
setResult(Activity.RESULT_OK, intent);
}else{
getParent().setResult(Activity.RESULT_OK, intent);
}
finish();
super.onBackPressed();
}
My AndroidManifest.xml has the following intent filter for Activity2:
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
I verified that my launchMode is standard (and not singleTask, etc) as advised here and my request code is not negative as warned here. I also tried android:launchMode="singleTop", but that was a no-go also.
I also tried not calling finish() in onBackPressed() for Activity2 as mentioned here (also with just super.onBackPressed() as suggested here) and again calling it as suggested here.
Additionally I tried commenting out the line intent.putExtra("Stuff", someStuff); as it seemed to cause trouble for this person.
Any ideas as to what I might be doing wrong?
So here is the final solution that took care of it:
I changed the intent for Activity1 to the following:
Intent myIntent = new Intent();
myIntent.setClassName("com.myProject", "com.myProject.Activity2");
startActivityForResult(myIntent, 600);
For some reason Android requires the fully qualified name for the second parameter in addition to the package name given by the first parameter. Now it works! :)
It will happen if "singleInstance" flag is set when you launch the activity.
Not certain what your problem is. The way you're creating the Intent in Activity1 is odd; that method isn't meant for creating intents that launch another activity in the same app. Some developers use the Intent(Context, Class<>) constructor. I prefer to use Intent(String action) with a custom action string defined only in my app (which is easier to code correctly).
Also, the intent filter you've specified for Activity2 is usually used for an activity that's launched directly from the Home screen.
Where's the onCreate() code for activity2? Where's the code for onBackPressed()? Can you prove to me that setResult() is called before some other code in Activity2? You should run the activities in debug. Ensure that Activity2 is receiving the intent you think it should, then trace step by step the statements that are executed until setResult(). The thing not to do is throw solutions at the code before you understand what the underlying problem is.
As far as I can tell so far, Activity1 is sending out an Intent, and then onActivityResult is being called. Nothing else is proven so far.
What is the difference between starting ActivityB from ActivityA using
1. startActivity(this, ActivityB.class);
versus
2. startActivity(getApplicationContext(), ActivityB.class);
I typically see 1. used more often in examples, but I haven't come across a reason for why this is the case.
Reference to Activity as a Context (this) might become obsolete if your Activity goes through configuration changes, like rotation, and is destroyed and created again. Context recieved by getApplicationContext(), however, persists through lifetime of the process.
However, It seems to me it only is an issue when you bind Activity to Service or other similar scenario, so it's safe to use this when you use it in intent to start another Activity.
There is no difference. According source code of Intent and ComponentName - only thing, that used form context - is getting package name by context.getPackageName(). Package name is the same for Activity.this and Activity.getApplicationContext(), so there is no difference.
I assume you are actually asking about the difference between
startActivity(new Intent(this, ActivityB.class));
and
startActivity(new Intent(getApplicationContext(), ActivityB.class));
There is no difference. Android needs the ComponentName (package name and class). The context is used to determine the package name.
I have a common menu on my app with icons. Clicking an icon will start an Activity. Is there a way to know if an activity is already running and prevent it from starting multiple times (or from multiple entries)? Also can I bring an activity that is in onPause state to the front?
Use this:
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
while starting Activity.
from documentation:
If set in an Intent passed to Context.startActivity(), this flag will
cause the launched activity to be brought to the front of its task's
history stack if it is already running.
In your activity declaration in Manifest file, add the tag android:launchMode="singleInstance"
I got it perfectly working by doing the following.
In the caller activity or service (even from another application)
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(APP_PACKAGE_NAME);
//the previous line can be replaced by the normal Intent that has the activity name Intent launchIntent = new Intent(ActivityA.this, ActivityB.class);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launchIntent);
and in the manifest of the receiver activity (the I want to prevent opening twice)
<activity android:name=".MainActivity"
android:launchMode="singleTask"
>
This works for me :
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
from official documentation
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
also, you can use FLAG_ACTIVITY_NEW_TASK with it.
then the code will be :
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Just use
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
create an instance of your activity which you dont want to start multiple times like
Class ExampleA extends Activity {
public static Activity classAinstance = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
classAinstance = this;
}
}
Now where ever u want to crosscheck i mean prevent it from starting multiple times, check like this
if(ExampleA.classAinstance == null) {
"Then only start your activity"
}
please add this in menifest file
<activity
android:name=".ui.modules.profile.activity.EditProfileActivity"
android:launchMode="singleTask" // <<this is Important line
/>
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...!!!