Skip to content

Instantly share code, notes, and snippets.

@samtstern
Created November 22, 2016 16:44
Show Gist options
  • Save samtstern/2705b8c56c86622033d6c30478fdd878 to your computer and use it in GitHub Desktop.
Save samtstern/2705b8c56c86622033d6c30478fdd878 to your computer and use it in GitHub Desktop.
Invisible Activities
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.firebase.quickstart.invisibleactivitytest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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=".InvisibleActivity"
android:theme="@style/AppTheme.Transparent" />
</application>
</manifest>
package com.google.firebase.quickstart.invisibleactivitytest;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import java.util.Random;
public class InvisibleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invisible);
new AsyncTask<Void, Void, Integer>() {
@Override
protected Integer doInBackground(Void... voids) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new Random().nextInt(1000);
}
@Override
protected void onPostExecute(Integer result) {
Intent data = new Intent();
data.putExtra("RESULT", result);
InvisibleActivity.this.setResult(RESULT_OK, data);
InvisibleActivity.this.finish();
}
}.execute();
}
}
package com.google.firebase.quickstart.invisibleactivitytest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivityForResult(new Intent(this, InvisibleActivity.class), 1000);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000) {
int result = data.getIntExtra("RESULT", -1);
Toast.makeText(this, "RESULT: " + result, Toast.LENGTH_SHORT).show();
}
}
}
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment