How to launch an Activity without a UI?
You need to add the Intent flag,
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Or
call "finish();
" after firing the intent.
Must every activity have a layout?
The answer is yes it's possible. Activities don't have to have a UI. It's mentioned in the documentation, e.g.:
An activity is a single, focused thing that the user can do. Almost
all activities interact with the user [...]
(see http://developer.android.com/reference/android/app/Activity.html)
Related SO question: https://stackoverflow.com/a/12817384/534471
To e.g. display a Toast from an Activity without layout you would define the activity in your manifest like so:
<activity
android:name=".MainActivity"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The code would look like this:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "I'm alive", Toast.LENGTH_LONG).show();
finish();
}
}
Start activity without showing it
I found the solution:
- Activity A starts activity B with extra parameter (intent.putExtra("something", true))
- Activity B:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().hasExtra("something") && getIntent().getBooleanExtra("something", false) {
//show activity B
setContentView(R.layout.activity_B);
} else {
//don't show activity B, start activity C
startActivityForResult(activity_C, ACTIVITY_NOT_INITIALIZED); //start activity C
}
}
//when came back from activity C, check if activity B was already initialized
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ACTIVITY_NOT_INITIALIZED) {
//show activity B
setContentView(R.layout.activity_B);
}
}
How to start android activity without showing main activity in the back
I solved my problem like this :
1- I added to the main activity attributes in the manifest file :
android:launchMode="singleTask"
I tested it does not makes loading time when re-launching activity longer than usually because this attribute does not erase the app cache and keeps the intent instance in the memory.
2- Add this to main activity class :
@Override
void onPause()
{
super.onPause();
finish();
}
Thank you all for your help.
Android Activity with no GUI
Your best bet would seem to be using a BroadcastReceiver
. You can create a new BroadcastReceiver that listens for the Intent to trigger your notification and start your service like this:
public class MyIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context _context, Intent _intent) {
if (_intent.getAction().equals(MY_INTENT)) {
// TODO Broadcast a notification
_context.startService(new Intent(_context, MyService.class));
}
}
}
And you can register this IntentReceiver directly in the application Manifest without needing to include it within an Activity:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.myapplication">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:enabled="true" android:name="MyService"></service>
<receiver android:enabled="true" android:name="MyIntentReceiver">
<intent-filter>
<action android:name="MY_INTENT" />
</intent-filter>
</receiver>
</application>
</manifest>
Related Topics
Does the Android Emulator Support Opengl Es 3.0
How to Get Email Id from Facebook Sdk in Android Applications
Mvvmcross Changing Viewmodel Within a Mvxbindablelistview
Android.Os.Networkonmainthreadexception . Need to Use Async Task
How to Show Daily Offline Notifications in Android 10
Broadcast Receiver Not Working in Ics If the App Is Not Started Atleast Once
How to Change Images on Imageview After Some Interval
Why Is My Field 'Null' After Injection? How to Inject My Object
Android SQLite Selection Args[] with Int Values
How to Create Custom Shape Button with Selector in Android
Out of Memory Exception Due to Large Bitmap Size
Embed Activity Feed of a Public Facebook Page Without Forcing User to Login/Allow
Getactionview() of My Menuitem Return Null
Google Play Services Out of Date. Requires 11011000 But Found 10289574