Android App - disappearance of app GUI - android

I'm trying to create a simple app, whose main task is to open the browser on defined URL.
I've created first Activity:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Intent myIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://my.url.tld"));
startActivity(myIntent);
}
Here's my AndroidManifest.xml:
<manifest ...>
<application ...>
<activity android:name=".MyActivity" ...>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
This code is fully functional, but before it opens the browser, it displays a black background - blank app GUI. I didn't figured out, how to go directly do the browser (without displaying the GUI).
Anyone knows?

Add this theme declaration to your activity definition in you manifest
<activity android:name=".MyActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
>
Be careful with this, you must call finish(); from your activity after you are done with it otherwise users could get stuck with an invisible activity that prevents them from interacting the phonetop.

Related

Close/Kill Android application from recent application on soft key press

I have a overlay view over all apps. I want to close the overlay view from screen on soft key press (home, back and recent). I have a transparent activity with no content which is starting a service having overlay view.
public class SampleOverlayShowActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this, SampleOverlayService.class));
}
#Override
protected void onPause() {
SampleOverlayService.stop();
finish();
super.onPause();
}
}
Inside manifest
<activity
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:name="SampleOverlayShowActivity"
android:excludeFromRecents="true"
android:theme="#android:style/Theme.Dialog" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Everything is working fine expect recent apps. Activity still show up on recent application button press. It got removes when I again press recent application button.
This should be much simpler than trying to override soft key behaviour to do clean up. Instead make sure the activity is launched so that it has no recents trace. From the manifest that means adding noHistory and excludeFromRecents
<activity android:excludeFromRecents="true"
android:noHistory="true"
android:name=".."
...>
<intent-filter>
...
</intent-filter>
</activity>
Note that android:excludeFromRecents works for the task only, so you probably want to make sure that activity is a new task too, using the appropriate flags (android:launchMode="singleTask")

Does Android need a default layout file called activity_main.xml

I'm an Android noob and I'm having difficulty finding out something basic about Android.
I currently have an activity_main.xml file.
I want to use this layout when I first start the Android emulator, using Android Studio
Does Android look for a file activity_main.xml and use it as the default layout?
Here's my AndroidManifest.xml:
<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>
</application>
So I understand that this specifies that my .MainActivity will respond to an action.MAIN intent call. What I don't know is what the action.MAIN intent call actually is, and how my activity_main.xml relates to this.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Can anyone provide an explanation or a link to a good primer that explains these basic concepts?
From manifest :
<action android:name="android.intent.action.MAIN" />
Means this activity is the entry point of the application. In your case, the MainActivity starts.
The MainActivity sets up the layout for itself - the line responsible is
setContentView(R.layout.activity_main);
Where it looks for layout file activity_main.xml. This is just the naming convention - feel free to rename the layout file and call the new one from setContentView. It's not required to be called activity_main

Acitivity SingleTask behaviour with SplashScreen

In my App I have:
-SplashScreen (SSc) preparing the application (starting services and so on)
-MainActivity (MA) the most relevant part of the app handling most actions
-and some other activities which are not that relevant
For my App I'd like to have the behavior like launchMode singleTask, so that my App is always started as a new Task, even when opened through a link click in SMS/EMail app. The best would be to have only one Instance of my Activities as they are all serially navigable.
However when I start SSc as singleTask it is the root of the stack and navigating to the MainActivity, pressing home, click on the Launcher icon again the app is fully restarted. So SSc is shown again and so on. In this Situation, I would like the MainActivty to be brought to the front instead.
my wish would be:
launcherclick -> SSc ->MA ->HOME -> launcherclick -> bring MA to front -> HOME-> relaunch from recents -> bring MA to front
Click on link ->SSc/MA (whether it is first start) with the same instances
In my App it does not make sense to have multiple instances, as the background service only handles one MainActivity at a time because it polls data frequently just for the seen "Thing".
Do you have any recommendations to achieve this goal?
my first idea was a LauncherActivity with launchMode singletask without layout to route the intents to the other activities (which most likely will be singleTop !?, because its only in one task then) like:
public class LauncherActivity extends Activity {
private boolean firstStart = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected void onResume() {
super.onResume();
if(firstStart){
startActivity(new Intent(this, SplashScreen.class));
firstStart = false;
} else {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
}
}
Manifest xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="x.startintenttest">
<application
android:allowBackup="true"
android:allowTaskReparenting="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name="x.startintenttest.MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop"></activity>
<activity
android:name="x.startintenttest.MainActivity2"
android:label="#string/title_activity_main_activity2"></activity>
<activity
android:name="x.startintenttest.SplashScreen"
android:label="#string/title_activity_splash_screen"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="*.xyz.de"
android:pathPattern="/...-........."
android:scheme="https" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Its simple, I also did the same. I was using singleTask for my splash and the main activity. So that I faced the same issue(Splash was showing at every wakeup). But I solved this by removing the singleTask from the splash and kept it for the MA alone(I used to finish the splashActivity when the MA starts). Try this trick.
The solution provided Sripathi fixes the issue, but you end up with the link-opening-app having the splash screen layout in its preview. My solution to this is to have a LinkEntryPointActivity with no layout that then delegates the received intent to the splash screen.
class LinkEntryPointActivity : MyBaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intentClone = intent.clone() as Intent
val componentName = intentClone.component
val newComponentName = ComponentName(componentName.packageName, SplashActivity::class.java.name)
intentClone.component = newComponentName
startActivity(intentClone)
finish()
}
}
And declare it in AndroidManifest.xml as
<activity android:name=".ui.LinkEntryPointActivity"
android:launchMode="singleTask" <-- This forces to open the link in a new task
android:screenOrientation="portrait">
<!-- Intent filters here -->
</activity>
The original splash screen activity should still be handling the MAIN action in the LAUNCHER category as usual.

Want to launch another activity on start up page without using any listener?

Example I have a java class that has sliding menu code which the java name is Leftandright.java.
Then I have a Main_Activity, but without any listener I want the Application to launch at the LeftandRight.java page. How do I do that?
You can set the starting activity in manifest file, AndroidManifest.xml, by declaring that Activity as a main activity.
<activity
android:name="com.package.LeftandRight" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Or, you can add this code in Main_Activity.java (although this is a VERY BAD approach, do not use unless you know what you're doing)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(this, LeftandRight.class));
}

Difference between android 2.3.3 and android 4.2 enforcing permissions?

I am just playing with a certain scenario of inter-application-communication,
and trying to circumvent the problem that in Android the main activity of an application
can't be secured by a custom permisssion (as in that case it can't be launched at all).
I tried to use two activities instead: 1) The main activity which is not protected.
2) When a button is clicked, the main activity sends an explicit Intent to start the second activity.
That one performs some sensitive work and is protected by a custom permission
("toy.test.permission.ACTIVATE_SECOND_ACTIVITY").
The idea being that if the main activity is either started by the user from the launcher or
by a foreign maliscious application using an explicit intent, we can warn the user before he pushes the button to proceed.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
final Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent activateIntent = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(activateIntent);
finish();
}
}); ....
The Manifest.xml file looks like that:
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/second_activity_name"
android:theme="#style/AppTheme"
android:permission="toy.test.permission.ACTIVATE_SECOND_ACTIVITY" >
<intent-filter>
<action android:name="toy.test.action.ACTIVATE_SECOND_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
(In addition, the SecondActivity can be started from a friend application by an implicit Intent with
actionString="toy.test.action.ACTIVATE_SECOND_ACTIVITY" using the permission
"toy.test.permission.ACTIVATE_SECOND_ACTIVITY".)
Now the difference between android 2.3.3 and android 4.2:
On an android 4.2 device or emulator, the SecondActivity can be started from the MainActivity by clicking
the Start button without any problems, although the application does not ask explicitly for the permission
"toy.test.permission.ACTIVATE_SECOND_ACTIVITY".
However, on an android 2.3.3 emulator, the SecondActivity can't be started because of SecurityException:
FATAL EXCEPTION: main
java.lang.SecurityException: Permission Denial: starting Intent { cmp=toy.test/.SecondActivity }
from ProcessRecord{406827d0 405:toy.test/10034} (pid=405, uid=10034) requires
toy.test.permission.ACTIVATE_SECOND_ACTIVITY
I am confused: Was the permission enforcement changed between the two Android versions?
And is it on purpose that android 4.2 allows such situations, i.e. activation of the SecondActivity without permission?
(Of course, that would be useful.)
Thanks a lot for any answers,
puffin137

Categories

Resources