How to close old activity before opening next? - java

I am building an Android app and are using the Parse SDK for Android.
Currently I am subscribing to channels with this in my main activity:
PushService.subscribe(this, "User_1_channel", MainActivity.class")
This works fine except for one thing. It opens up MainActivity and it gets put upon the other MainActivity in the stack which is very bad. I really want to remove the old activity before the new launches. I know I can do it with this but I have no idea where to put it since the Parse SDK handles everything.
launch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
People say an alternative is to use a Custom broadcastreceiver. Problem with that approach is I have no idea how to subscribe to a channel without specifying an activity as the third parameter.

If you define the activity as SingleTask in your configuration file, it will indicate only that one activity will be used the Task.
android:launchMode="singleTask"

Related

Use .getWindow() in Application-file

This may be a silly question but I have the following situation: I want to setup my window layout every time the app is open, for example changing the status bar color. So I created my Application file because I heard it is better if you check something like this in the application file and not in the MainActivity.
Problem: How can I call the getWindow() method without an open activity.
Thanks for helping.
A bit tricky, but you can use Window manager in background-service to display your views without opening an Activity.
Also, for API Level 26+ (Oreo or above) you have to start your service as foreground service

Error: 'Default Activity Not Found' for an app containing a Broadcast receiver

I have two separate apps.
The first sends out two different kinds of Broadcasts.
The second one waits for this broadcast from App #1 and uses the broadcast to decide what activity to launch.
What I'm confused about is this: It seems to me that App #2 (the receiving app) shouldn't necessarily require a default activity since the activity it launches depends on the broadcast received from App #1.
But, if a default activity is not defined in App #2, it can't run and throws:
Error running app: Default activity not found
But, if I do define one in the manifest file, then this defeats the purpose of the Broadcast.
Please help me understand the bigger picture and maybe offer a solution.
UPDATE: The issue is solved. To the people facing the same problem, I found 2 possible ways to resolve this:
As suggested by #Lemi Miles, We can simply go to App>edit
Configurations> On launch options and choose Launch "Nothing".
Alternatively, you could create an activity with no functionality to
act as a launcher activity for the app
App>edit Configurations>
On launch options choose Launch "Nothing"
I had this problem since 3 days.. If you are sure that there is no problem in your manifest.xml then Here is a solution...
1)Delete Android.3.1 or which version folder you have from c/User/Android.your version...
2)Then start Android again..
It worked for me..
I had the same problem.
Try this: In Android Studio Tool Bar -> select Run->Edit Configuration-> On Launch Option ->Select Nothing in place of Default Activity

How to manage multiple activities with the same name inside an app

I am currently writing an Android app to work as an activity launcher.
The main activity works as launcher and will let users launches a new activity from the main activity(here I create this new activity using FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_MULTIPLE_TASKS).
So that I can hold multiple these activities with the same name but different content in the background.
But I still need a method to manage them, which means I can offer a UI for user to choose to restart one and kill one.
Can someone give me a good solution?
I appreciate your help very much!
And you can also view a similar question asked by me yesterday: set unique identifier for RunningTaskInfo
You need to get the list of Activity instances which are running in your application. List of all of the activities in our application that are running on the device gives an answer how to do this.

First android app for iOS developer

i am iOS app developer. Now when i created an app on iOS i want to do the same on android. With java i was familiar just now need some time to remember.
i was looking at some tutorial how to do one or other things. But what i can't find is the basics how everything works. How classes is interacting with each other.
For example i want to create registration window with few buttons and alerts.
I want registration window to be called just once when app is installed and just that.
Should i create new java class and few layouts, one for View with buttons and other for Alerts ?
Or should i create other class for alerts if i need them in other flow of my app ?
And how i should call that window with registration from my main class, which is
extends Activity
Also if there are some developers who came this road from objective-c (iOS) to java (android). It would be nice for some share experience how they did that.
Thank you for any help :)
Very few of the concepts in iOS and Android are similar. On Android you have Activites, Intendts the Manifest. When you design your layout it should be resolution independent. You have a search, back and a menu button and variable hardware. All of this has no equivalent in iOS.
That said, I think you just have to read the basic concepts and the getting started guide no matter if you come from iOS or never have done mobile development before.
EDIT
To answer your concrete question. Take a look at the lifecycle of an Activity and Preferences. With this, you could do some action on the first start of your main Activity and store some flag in the preferences when it's done. On the next start you just test that preference and skip the logic.
You can create one activity (.java file) and one layout(.xml file with buttons and input boxes) , alerts could be toast notifications:
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
All you require for this is a activity and a layout xml for that activity, this activity will be your main ie the entry point to your application, in that activity oncreate method you can check if it is registered or not by setting a flag or something which will direct to the next activity if its registered.
GOOD LUCK...
Just like your nibs in iPhone you create xml layouts in Android. And for view controllers here you make activity. One important thing is AndroidManifest.xml file, it contains all information of your app (like plist) plus all the activity information(Intent type and launcher methods).

How to prevent Android application from loading multiple times?

This must be an easy one but I'm not having much luck searching for an answer.
Apologies if this is a regular question.
If I navigate away from my app I cannot return to it. Starting the app again will load a second instance of it rather than returning to it. If I leave an audio loop running in my app, It's hard to get back in and turn it off.
On startup I'd like the app to destroy any previous instance of itself left running.
I'd also like to try having the app shut itself down when I navigate away (I know it's not the right way to do things but I'd like to try this for my own personal use of the app). Or have the "back" button destroy the app.
Thanks.
Add this to your activity definition in manifest...
android:launchMode = "singleInstance"
How to prevent the activity from loading twice on pressing the button
I have answered such a question,you can declare android:launchMode="singleTask" attribute for your MainActivity in the AndroidManifest.xml file, so that the OS won't create a new instance if there is one running in the background.

Categories