How to find out the reason why Activity doesn't start - android

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!

Related

How to make sure the MainActivity will be created only once

I have a react native project. And there is a service running in the background.
When the app is not running, the service is still alive, and it may want to start the MainActivity in some time. I’m using the following code to start MainActivity(I tried noFlag/addFlag/setFlag):
Intent Intent = new Intent(context, MainActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(key, value);
context.startActivity(intent);
The AndroidManifest.xml is declared like:
<activity
android:name=".MainActivity"
android:launchMode="singleTask”>
</activity>
Each time the MainActivity will be created twice. In the first time we can get the extra value, but the second time it will be empty.
How can I make sure the MainActivity will only be created once?
Thanks.
Just found the problem. My method breakpoint on startActivity worked.
The third party sdk cannot find property receiver, so it send a startActivity call with intent flag FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK, which overrode my singleTask in AndroidManefest.xml.
As you most probably have known, the problem lies with you calling the startActivity twice. I think it would be lovely if you could show the code that calls the startActivity.
I have just now tested that as long as you have singleTask or singleTop as your launchmode, OnCreate wont be called twice. As you can read from here : https://developer.android.com/guide/components/activities/tasks-and-back-stack
the function that would be called is onNewintent and if the putExtra is empty, it indeed can create a NPE
I hope that helps you, Good luck

Android Starting an Activity from different project

I have 2 projects in Eclipse. When I click on a button in project1, I want it to launch an activity from project2.
how can I do that ?
i tried the following code
Intent intent = new Intent(this,com.project2.tp02.MainActivity.class);
intent.setClassName("com.project2.tp02", "com.project2.tp02.MainActivity.class");
startActivity(intent);
I get an error message telling that first application stopped. How can I lauch it properly without such error?
Thanks in advance to any helper.
I got it work using this :
Intent intent = new Intent();
String pkg ="com.project2.tp02";
String clazz =pkg + ".MainActivity";
intent.setComponent(new ComponentName(pkg, clazz));
startActivity(intent);
I don't understand why that way it works and not previous one, if someone can explain I will be thankfull, but anyway I got my problem solved that way.
This is working code to invoke an activity which is in another project.
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.my.package", "com.my.package.MyClass"));
startActivity(intent);
I think your first code did not work because you provide your current activity as context of invoked activity where it is in another application.
<intent-filter>
<action android:name="com.project2.tp02.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Adding the above lines in the Manifest of your first activity might solve your problem.
It would be interesting what the error message is saying exactly... you do need the intent-filter as described by Swayam on the activity you want to call.
Try
new Intent("com.project2.tp02.MainActivity");
to get the intent, instead of setting the class name.

Start multilple activity from broadcastReceiver

I want to start multiple activities from my broadcast receiver. I have two classes i.e ReadContacts and CallDetails. I want to start them one by one. like first calldetails activity should be started and then next. I have tried below code and it works fine.
Intent calldetails = new Intent();
calldetails.setClassName("com.simplereader", "com.simplereader.Calldetails");
calldetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(calldetails);
then I tried below code to start other activity
Intent readcontacts = new Intent();
readcontacts.setClassName("com.simplereader", "com.simplereader.ReadContacts");
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(readcontacts);
But its not working and application crashes.
You must have the Intent Flag Intent.FLAG_ACTIVITY_NEW_TASK to start an Activity from outside of an Activity context so you need to add that flag to your second Intent.
I don't know if this is your only problem but if that doesn't fix it then post your logcat so we can see the error.
Intent readcontacts = new Intent();
readcontacts.setClassName("com.simplereader", "com.simplereader.ReadContacts");
calldetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // you need this flag
context.startActivity(readcontacts);
FLAG_ACTIVITY_MULTIPLE_TASK Do not use this flag unless you are implementing your own top-level application launcher.
From the android developer documentation for intent.
You could probably just launch both activities with the new task flag.
I think you are making mistake in this line
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
If you want to start readcontacts activity it shoul be
readcontacts.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
instead of
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
I think this is the reason.

Start Multiple Instances of an activity from BroadcastReceiver

I want to create multiple instances of an activity from BroadcastReceiver, the activity contains a AlertDialog, currently I am using the following code for this purpose:
Intent intent = new Intent(this, MultipleInstanceActivity.calss);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
and in manifest file android:launchMode="standard" somehow I think this FLAG_ACTIVITY_NEW_TASK causing the android:launchMode="standard" to change to android:launchMode="singleInstance" or something. I am not able to create multiple instances of this activity. I also tried to use FLAG_ACTIVITY_MULTIPLE_TASK, but no use.
I have created a PreferenceActivity, what really puzzles me is that when this PreferenceActivity is open my app creates multiple dialogbox i.e multiple instances with different data on it. But when its not open, my app wont create multiple instances just to make clear, it wont open another dialog. Logcat is not giving any warnings or errors.
My questions:
How to create multiple instances of an activity from BroadcastReceiver?
Can someone explain me whats happening in the second case, the PreferenceActivity one, why is it creating multiple instances?
I ran into the same problem as you have here, and i solved it by Using both FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_MULTIPLE_TASK.
Intent intent = new Intent(context, YourActivityClass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(intent);
Hope this works for you too.

How to call onNewIntent

I want to call onNewIntent when the first intent on onCreate was finished and the browser was closed, but I don't know can I do this. I found in android developers and goggling but I don't understand.
Can someone show me an sample example? thanks
You can use,
Intent in=new Intent(yourcurrentactivity.this, secondactivity.class);
starActivity(in);
For open secondactivity class from yourcurrentactivity.
and also add activity tag on manifest file.

Categories

Resources