Random Activity opens instead of Launcher Activity - android

After exiting from my app if i open it again random activities open instead of Launcher Activity. The problem continues even after putting in manifest. I have set the category as DEFAULT as shown in the code below. In the main activity i have put the code (given below) to exit the app on back press.
//Android Manifest -- i have set all activities as DEFAULT except LAUNCHER activity.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
#Override// In Main Activity I have put this code to exit app when back is pressed
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}

Have you tried setting the category to LAUNCHER like this
`<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
`

Related

Firebase click_action in notification payload work fine but it causes second time to launch same activity specified in click_action

I was study about Firebase's click_action use to open activity on notification click when app was killed. It's work fine! But after closing app open app normally(Not from notification) but its open same activity as specified in click_action.
Here is the code in manifest for launch screen :
<activity
android:name=".activity.SplashScreen"
android:screenOrientation="portrait"
android:theme="#style/LoginScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
And for Firebase notification :
<activity android:name=".activity.RequestsActivity">
<intent-filter>
<action android:name="RequestsActivity" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Code for Application exit when get back to home screen then :
#Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
Flow of current screen
Notification->Click->RequestActivity->Back_Click->HomeScreen->BackClick->Exit_App->Reopen_App->RequestActivity(But it must be HomeScreen!).

How to make an activity above the MainActivity.java?

My current MainActivity is a navigation drawer.
I want to make another activity on top of the navigation drawer.
Lets make that activity StartActivity.
On StartActivity there is a start button.
What i want to do is make the StartActivity opens up first when the app runs.
And when the user presses the START button on the StartActivity, it will direct him/her to the navigation drawer.
is this possible?
Create the StartActivity.
somewhere call "startActivity(intentforMainactivity);
go to the manifest and move
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
from the
<activity
android:name="sehtestapp.MainActivity"
into
<activity
android:name="sehtestapp.StartActivity"
Your Application will then start the StartActivity first
1) Create another Activity, that you'll call StartActivity
2) Set up a layout that you'll use in your StartActivity with a
button in it.
3)Create the onClickListener to launch a new Intent when clicked.
Make it launch the MainActivity.
4) Change your AndroidManifest as follow :
<activity
android:name="XXX.StartActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="XXX.MainActivity"
android:parentActivityName="XXX.StartActivity" />
You can try changing the Launcher activity and send intent having a boolean bundled in it when START button is pressed, in MainActivity fetch the intent, read that boolean value and open the drawer programmatically.

Reload Just the Main Activity not the Launcher Activity

I'm using this code to refresh MainActivity.java when a Refresh button is pressed.
Intent intent = getIntent();
finish();
startActivity(intent);
MainActivity.java has the category default and there is another Launcher activity. So whenever I press the Refresh button the Launcher Activity also starts again. I only need to start the MainActivity class. Below is the manifest
<application
android:allowBackup="true"
android:icon="#drawable/play_icon"
android:label="#string/app_name"
android:theme="#style/CustomActionBarTheme" >
<activity
android:name="com.theanilpaudel.joshilo.FirstScreen"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.theanilpaudel.joshilo.MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
What you are looking for in this case is an explicit Intent that launches MainActivity.
You can do this like so:
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
This will ensure that the Intent only launches MainActivity.
However, I highly recommend re-evaluating why you need to completely restart the Activity to refresh it. There are much cheaper (faster and less resource intensive) methods of updating data displayed in an Activity.
I guess I know the culprit. Try using the following code once.
Intent intent = getIntent();
startActivity(intent);
finish();
As far as I know startActivity() uses context to run. In case if you call finish() before startActivity() that may lead to destroy context of MainActivity and will get the application's context i.e. fires the activity with behavior LAUNCHER or MAIN.
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Let me know if it helps or you still have difficulty reloading activity.
Cheers

android how to start the main activity when I am on another activity

I am on signin activity and if the user click a button I want to start the main activity.
this is the mainfest for the main activity welcome
<activity
android:name=".Welcome"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I tried like this
Intent welcomeIntent =new Intent("android.intent.action.MAIN");
startActivity(welcomeIntent);
but i got a screen saying complete acting using with many choese , what is the solution pelase?
Change your intent to directly launch your class
Intent welcomeIntent = new Intent(context, Welcome.class);
startActivity(welcomeIntent);
If you're in an activity replace context with this

Open only one activity, without the main activity

I have an application that has one service and 2 activities.One activity (the main one) is a preferences activity and the other one is a dialog themed activity. The service from time to time needs to open only the dialog themed activity (using FLAG_ACTIVITY_NEW_TASK).
The problem is that when the preferences activity is opened in background (for example the user pressed HOME key instead of BACK, so the activity is OnPause) and the service tries to open the dialog themed activity, also the main activity is opened (comes in foreground).
I don't want this. So, how can I open only the themed activity from the service, without the main activity pop up ?
Android Manifest .xml
<service android:name=".SimpleService"> </service>
<activity
android:name=".Preferences" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".popup_activity" android:theme="#android:style/Theme.Dialog" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="screenon.popup.activity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
And from the service :
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory("screenon.popup.activity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I believe you're looking for the android:launchMode attribute.
Alex , I believe that all you want is to display preferences activity just once & then it must die(or lets say finish()) .
In that case you can override onResume of the preferences activity
to finish your preferences activity after it is resumed after pause
#Override
public void onResume()
{
//if activity is resumed after onPause then only run it
this.finish(); //simply kill your activity if it is resumed after pause
}
EDIT-To know whether the activity was paused you need to override onPause().I think you can dig out the rest.
Hope it helps!

Categories

Resources