Main activity inside trusted web activity - android

I started to use Trusted Web Activiy and everything is fine.
Now I want to install Push Notification library and my push ntification service provider needs to add a block of code in MAIN ACTIVITY.
From other hand, I need some check before user start visiting my website like:
Does She/He have an Interet Connection
Is Her/Him google chrome up to date
Get Her/Him device Id
and etc...
So, I need manuplate a splash screen and Main Activity, but I don't know where is main activity to do stuff as I'm newbie to this.
Would you please let me know how should I solve this issues?
Thanks in Advance

You can add a new activity, say MainActivity, which will start at application startup instead of TWA activity and do any additional processing in it's onCreate() method. In the end of onCreate() method you can activate your TWA activity with the intent like this:
startActivity(new Intent(this, com.google.androidbrowserhelper.trusted.LauncherActivity.class));
To make MainActivity the one which opens at application start you will have to remove intent filter from your TWA activity in AndroidManifest.xml and place it into your MainActivity:
<activity android:name=".java.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Some more details in this post: https://stackoverflow.com/a/58069713/8818281

Related

Android handling click on notification

I have this situation in my app: I have these activities
<activity
android:name=".presentation.view.start.view.activity.StartActivity"
android:screenOrientation="sensorPortrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".presentation.view.main.view.activity.MainActivity"
android:configChanges="locale"
android:screenOrientation="sensorPortrait"
android:theme="#style/AppThemeMultiStep"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity
android:name=".presentation.view.firstScreen.view.activity.FirstScreenActivity"
android:screenOrientation="sensorPortrait"
android:theme="#style/AppThemeMultiStep"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity
android:name=".presentation.view.signup.view.activity.LoginViaPinActivity"
android:screenOrientation="sensorPortrait"
android:theme="#style/AppThemeMultiStep" />
StartActivity acts as a sort of "router" based on the application state:
If I am not logged in, it will launch FirstScreenActivity
If I am logged in, it will launch LoginViaPinActivity, which will login the user based on some logic and then launch MainActivity. At the end of all, MainActivity will be at the root of the activities stack.
At some point the app will receive a notification, and when I tap it I want this:
if the app is running and MainActivity is running, open MainActivity (this is easy, there are planty of ways I can to that with various flags) but if it's not running launch StartActivity (so that I can open the app based on all the startup logics implemented there).
So the options I can think of are:
know if an activity is running in order to fire an intent or another (I don't like static fields solutions like you read in many SO post related to this)
make StartActivity the root of the task and find a combination of intent flags which will act like "launch StartActivity, but if it is running at the root of a task, bring that task to front" (this would be my preferred option if possible)
any suggestion is very appreciated
How do you usually handle this kind of situations? (I don't think I'm the first in the world with this problem :) )
Here is my approach -
Make StartActivity as your router as you've said. Just make launchmode to singleTop in your manifest in order to user onNewIntent() method of an Activity.
You'll generate notification, sending some data with contentIntent - as a result clicking on notification you'll be redirected to StartActivity.
Two cases here -
If StartActivity is in stack
onNewIntent gets called with your new Intent - Choose your activity required to be open - If it is already in stack Bring it to front using FLAG_ACTIVITY_REORDER_TO_FRONT flag
If StartActivity is not running or not in stack
Receive bundle of intent that is being got from the intent via notification, parse data and open an activity accordingly.

Start main activity when opening backgrounded app from other app by url scheme

I've just implemented deep linking to my app. I've added intent filter to my main activity. When user starts my url scheme I'm parsing it and handling in the activity to display proper information depending on parameters. It works well when my app is started from scratch.
<activity
android:launchMode="singleTask"
android:name="com.my.app.ui.activities.MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|screenSize|">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="myapp-scheme"/>
</intent-filter>
</activity>
The problem occurs when user opens some other activities on stack and goes background. Now if this app is resumed from background by calling my scheme url, all activities are still on the stack and the main activity doesn't show up.
How can I resolve this?
I think about navigating back to the main activity, but I don't have info if app was started from other app or in the normal way.
You need to make your MainActivity as singleTask by adding following line in the manifest(for ManiActivity):
android:launchMode="singleTask"
Then, override the onNewIntent() in the MainActivity and handle the navigation based on deep link data.Hope this answers your question.

android "which application you want to use" dialog when redirecting to an activity

these are the 2 intent filters of my 2 activities:
StartActivity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
HomeActivity
<intent-filter>
<action android:name="android.intent.action.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
I'm in the home activity and when I call:
activity.startActivity(new Intent("android.intent.action.MAIN"));
Android shows a dialog that askes which application I'd like to use to go on.
Someone is so kind to explain me why??
EDIT
I'm gonna be clearer:
StartActivity -> Contains login e and signin actions
HomeActivity -> Contains the most part of the app
My scenario:
I successfully logged in and now I'm in the HomeActivity, but when I log out and redirect to start activity android shows a dialog "With wich application do you wanna complete this action?" (or something like this) instead of redirecting me to the StartActivity
I don't know why you need a Action HOME but it looks like your problem is the Category DEFAULT. Due to the android docs this category is defined as :
Set if the activity should be an option for the default action (center press) to perform on a piece of data. Setting this will hide from the user any activities without it set when performing an action on some data. Note that this is normally -not- set in the Intent when initiating an action -- it is for use in intent filters specified in packages.
Constant Value: "android.intent.category.DEFAULT"
So I think changing this one to the right one which fits your need will solve the problem. Just a suggestion!
Read more at: http://developer.android.com/reference/android/content/Intent.html#CATEGORY_DEFAULT
Not sure if it's resolved, but I avoided the problem using
activity.startActivity(new Intent(activity,StartActivity.class));

Newly added Activity designated as Main is not always "Main" Activity shown

Allow me to explain my predicament.
I have been building an app for a while now where it would immediately enter my MainActivity.class. I had this Activity declared in the AndroidManifest.xml file as such:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Then I realized that I wanted to add a Login Activity to my app so a NewMainActivity.class was required. I've been able to successfully implement this new Activity. And I changed the <intent-filter> of the old MainActivity.class to the following:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
However this is where my problem arises.
When I press the back key on the Login Page (NewMainActivity.class) it goes through the onPause(), onStop(), and onDestroy() methods. When I start the App again the Login Page (NewMainActivity.class) is not shown, the old MainActivity.class is shown instead.
Can anyone guess what I'm still missing/doing wrong? It seems to me like it should always start on the Login Page everytime now. Let me know if a code sample would help.
Delete from your old MainActivity.class the intent-filter that you've defined:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Update: I've understood your question incorrectly at first. The intent-filter I've proposed to delete does nothing except exporting your activity. This means that any application can call the activity of your MainActivity.class So, in any case it should be deleted.
Now about the login activity. As I understand your scenario is the following. When you start your application it at first starts login activity. Then you enter your credentials and the main activity is appeared if the login process has been successful.
Thus, you have two activities in the stack: login and your main class. When you then push home button and run your application once again it starts from the last activity in the stack i.e. Main activity.
Thus, the problem is not in your intent-filters. You should finish your activities. In this case you'll always start from the login activity.
If at first Yury's suggestion does not work, and you did something similar to what I did, which was copy and pasting your workspace between multiple computers and loading the project to eclipse each time, then simply start a new project and copy the actual text within all your classes to fresh new classes in the new project.

How to launch a Android Service when the app launches?

I'm still fresh to Android and Id think the below config works for launching my service when the app launches.
<service android:name=".PlaylistUpdaterService">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</service>
But this is not the case. What did I miss?
Wrong! extend the Application class (IE make your own), then in the onCreate() method do this.
//Service is below
Intent serviceIntent = new Intent(getApplicationContext(), PlaylistUpdaterService.class);
startService(serviceIntent);
And take that intent filter crap out of your declaration in the manifest file. Leave it as
<service android:name=".PlaylistUpdaterService">
The intent filter following needs to be in your home activity only
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
The reason you do this is because the Application class is started up as soon as the app is, and acts as kind of a global class the the android framework manages.
Actually if you want the service to run every time you go back to your home screen, you should start the service in your home classes onResume(). Putting it in the applications onCreate() will only start the service if the user is launching for the first time, or after the running process has been killed. Or you could put it in your home classes onCreate() but that is not even guaranteed to run every time.
Correct me if I am wrong, but android.intent.category.LAUNCHER is valid only for Activity. So, does not look like valid way to start Service. The same you can achieve if you do the following:
create transparent Activity that will be used only to start Service
for that Activity, you do not need to specify GUI layout. So, you do not need to setContentView() in the activity's onCreate(). The only thing you need is to put
#android:style/Theme.NoDisplay
under Theme tag for this Activity in AndroidManifest.xml.
start Service from onCreate() of your Activity.
call finish() in onStart() of your Activity to close it.
So, your Activity will be invisible to the user, last shortly and nobody will notice that it was used to start the service.

Categories

Resources