Android: dialog started by ACTION_REQUEST_ENABLE destroy the previous activity - java

I want to enable bluetooth with the following code from an intent service:
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
getApplicationContext().startActivity(enableBtIntent);
The logic around my application is:
Activity A launch Activity B which launch this ENABLE_REQUEST.
I tried to launch this request from a service and after even within Activity B itself, but Activity B is always destroy and Activity A comes foreground.
Here's the manifest.
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ClassifierActivity"
android:screenOrientation="portrait"
/>
<service
android:name=".audio.SpeechRecognitionService"
android:exported="false"
android:description = "#string/speech_recognition_service_description"
/>
<service
android:name=".interaction.SmartObjectInteractionService"
android:exported="false"
/>
I couldn't find any solution!
P.S. Activity B has a Camera Fragment.

try using startActivityForResult instead of startActivity

Related

Figuring out the launcher on this Android Studio Github project

Still a little bit new to Android studio and app development. On this project: https://github.com/watabou/pixel-dungeon, which file is acting as the MainActivity.java file? As in, which file is the main launcher of the application, how do you tell?
The activity with the intent filters android.intent.action.MAIN and android.intent.category.LAUNCHER is the launcher for any app,
Just check which activity it has bellow intent filter that is launcher,
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
In that project PixelDungeon is the launcher activity.
Your launcher activity is defined in your android manifest (AndroidManifest.xml), it should look like this:
<activity
android:name=".activity.Login_screen"
android:label="Activity Validation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
For your case, if you want to edit your launcher activity just change ".activity.Login_screen" for the activity you want to display on launch. However, make sure that there are no duplicate definitions.
As you can see in your code:
<activity
android:label="#string/app_name"
android:name=".PixelDungeon"
android:screenOrientation="portrait">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

ActivityNotFoundException not resolving

I want to open activity via broadcast receiver , i tried any ways , but i am getting ActivityNotFoundException , my activity is working on normal mode, but when i want to open it from BroadCastRecevier it cause ActivityNotFoundException error,
It is my manifest ,
<activity
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:name="com.alexis.abc.ui.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And it is my broadcast receiver ,
Intent intent2 = new Intent(context, MainActivity.class);
intent2.addCategory("android.intent.category.LAUNCHER");
intent2.setAction("android.intent.action.MAIN");
context.startActivity(intent2);
Here is steps :
1 - I opening application and hiding launcher icon via following code
PackageManager packageManager = getContext().getPackageManager();
ComponentName componentName = new ComponentName(getContext(), MainActivity.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
2 - I exiting to application and dialing a number (To triggering broadcast event) and i getting following exception
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.alexis.abc/com.alexis.abc.ui.MainActivity}; have you declared this activity in your AndroidManifest.xml?
Do you have a permission like this:
<receiver
android:name="Your receiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="action"/>
<category android:name="category" />
</intent-filter>
</receiver>
inside your Android Manifest file? If not, then try to open your Android Manifest file and put something like what I've posted above.
Or try .sendBroadcast() instead of .startActivity() on the last line of your code:
Intent intent2 = new Intent(context, MainActivity.class);
intent2.addCategory("android.intent.category.LAUNCHER");
intent2.setAction("android.intent.action.MAIN");
context.sendBroadcast(intent2);
Change your code which you want to open the application and hide the launcher icon:
ComponentName componentName = new ComponentName(getContext(), YourReceiver.class);
Don't forget to declare your BroadcastReceiver in manifest:
<receiver
android:name="YourReceiver" >
<intent-filter>
<!-- your intent filter goes here -->
</intent-filter>
</receiver>

Trying to have main menu and submenu classes

So my original problem was that in my manifest my menu wasent loading ie
<activity
android:name=".MainMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.MAINMENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This was fixed by loading it at startup ie
<activity
android:name=".MainMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
So now my main menu works. However, the buttons inside MainMenu.xml will take you to another .xml file with more buttons. So now I have the same problem. I created another class called SubMenuChapter3 and put it in the manifest as such.
<activity
android:name=".SubMenuChapter3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.learnar670_1.SUBMENUCHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Now this doesnt work I am assuming for the same reason as before with mainmenu. Doesnt crash or give me errors. It just wont open the submenuchapter3 class. I forced the submenuchapter3 class to open by putting
startActivity(new Intent("com.th3ramr0d.learnar670_1.SUBMENUCHAPTER3"));
directly into the MainMenu class outside of an onclick just to see if it was working. When I do that it opens the chapter_3.xml like it is supposed to and the button works. Thanks for the help.
You misunderstood <intent-filter> tag and the way you start activities.
Also maintain proper terminology - Menu and Activity are completely different things.
Everything you need to know about Activities can be found here: Activities | Android Developers
Example:
This entry in AndroidManifest.xml says "show the MainMenu Activity as icon in the launcher":
<activity
android:name=".MainMenu"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
EDIT: This allows Android to start MainActivity. It will also show in the installed app list.
The following Activity will not be displayed in launcher but can be opened from the app:
<activity android:name=".SubMenuChapter3"/>
EDIT: This allows Android to start SubMenuChapter3. It won't show in the installed app list.
These lines say "open the SubMenuChapter3 Activity":
Intent i = new Intent(this, SubMenuChapter3.class);
startActivity(i);
EDIT: You call this code from inside the onClick method inside MainMenu. It will launch SubMenuChapter3.

android app crashing on startActivity()

I have started an Intent and asked it to go to the main activity, when it attempts it the app crashes.
Here is the code that tries to go to the main activity.
Intent i = new Intent(
".MAIN_ACTIVITY");
startActivity(i);
Here is the XML manifest for Main_Activity.
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I'm still pretty new to this so any help and/or advice is of great value.
Write like this :
Intent i = new Intent(MainActivity.this, NewActivity.class);
startActivity(i);
Also you need to declare both activity class in manifest file like this:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".NewActivity"
android:label="#string/app_name" >
</activity>
For those, who come from Google, I was trying to pass large string in putExtra (over 90K Symbols) and my app was crashing because of that. The correct solution is either save the string to file or implement Singleton.
Here is the relevant link Maximum length of Intent putExtra method? (Force close)
as per your code: if i have create newActiviy in my project then:
i have to add that activity in android manifest file.
like:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<activity android:name=".newActivity"></activity>
</activity>
for calling that activity just do:
Intent intent = new Intent(MainActivity.this, newActivity.class);
startActivity(intent);
befre ask question here try some googling. and you must have to check this: Building Your First Android App and Starting Another Activity
Start new Activity like this:
Intent intent = new Intent(YourCurrentActivity.this, TargetActivity.class);
startActivity(intent);

Make home intent take to specific application activity?

I've built a project that contains different packages and activities, say:
com.example.package1.Activity1
com.example.package2.Activity2
The first package holds a launcher. On my project manifest file this activity is listening to the home intent:
// Launcher
<activity android:name=".package1.Activity1" android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
// package2.activity1
<activity android:name=".package2.activity1" android:label="#string/package2_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
My problem is that when a user clicks Home the launcher isn't opened, instead the user is taken to the last opened activity of the app. EG: User opens package2.MainActivity from default launcher > goes to another app > clicks home > package2.MainActivity is opened again.
Is this normal? How do I make sure the device Home button takes to .package1.activity1?
If you are trying to create a HOME-screen replacement, then the activity for that needs to have the following in its <activity> tag in the manifest:
android:launchMode="singleInstance"
This will ensure that only one instance of this activity ever exists and that when this activity launches other activities that they will all go into new tasks and not be a part of the HOME-screen replacement's task.
I am not sure but please try to this code:
// package2.activity1
<activity android:name=".package2.activity1" android:label="#string/package2_name" >
<intent-filter>
<action android:name="android.intent.action.View" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Categories

Resources