How to close all child activities from parent in android - android-layout

I'm new to android. Actually one handler is running in Home Activity A for every 30 sec to check the net connection.
If I'm went to activity C by A->B->C, If there in no net connection at that time, then i want to close Activity B and C, then want to show message box in Activity A.
But My problem is My handler is running for every 30 sec in Home Activity A. But If i was in Activity C or some other Activity how to find which activity is my Application currently focussed now. Then i want to finish those child activities and want to show Home Activity A I have some 9 child activities in Activity B.
I heard about using "FLAG_ACTIVITY_CLEAR_TOP" . I used the code as follows in the handler in Home activity A itself. But got error.
Intent intent = new Intent( ctx, Homepage.class );
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity( intent );
Here Homepage.class is my Home Activity A and i set that activity in manifest file as
android:launchMode="singleTop"
Please help!

You can start Activity A and close all other activities.
You have to create new intent and add flag FLAG_ACTIVITY_CLEAR_TOP
Intent activityA = new Intent(context, ActivityA.class);
activityA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.StartActivity(activityA);
this will close all activities that are in the stack and are at the top of activity A

When calling Acitivity C, call finish in activity B , when calling Activity A from C , call finish() in activity C !

You can use, According to me two ways,
If you start an activity using startActivityForResult, then you can call finish() in this new Activity when you're done with it and it will return control to the activity that started it.
OR otherwise, May be I am wrong,
Call finish inside onStop 'override method'.

make a uniform resultCode for closing child activities. Eg. you make 911 (should be int) as your resultCode. If you want your Activity to finish and go back directly to parent Activity, you set the resultCode to 911:
setResult(911); finish();
In every child activity, you override the onActivityResult and check if the resultCode is 911. If yes, then call the setResult(911); finish(); until you get back to your parent activity. Hope this helps!

For example, if you want to start intentB, you can do following in old activity:
Intent intentB = new Intent();
intentB.setClass(XYZ.this, abc.class);
intentB.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentB);

Related

Why does the App stop working on activity change?

first question here by a Kotlin and AndroidStudio noob.
I made an app and want to go from one activity to another. I use buttons to navigate from the MainScreen to the 2 different screens. In Main I have this:
val firstBTN: Button = findViewById (R.id.firstBTN)
firstBTN.setOnClickListener {
val intent = Intent(this, FirstActivity::class.java)
intent.putExtra("Collection", collection as Serializable)
intent.putExtra("Result", result)
startActivity(intent)
collection is a MutableListOf objects called Edibles (String, Double) that I want to give to the next activity
it is then received by this:
val intentE = getIntent()
var collection = intent.getSerializableExtra("Collection") as MutableList<Edibles>
var result = intent.getDoubleExtra("Result",0.0)
But everytime I press firstBTN the App stops working.Unfortunately I don't even get any bug report, so I have no idea what's the problem. I thought it might be the "as MutableList" part, but I even created an element before passing to the next activity and the same happened...
Thank you already for your help!
This could be anything. You should provide more data.
For starters your Android Manifest. Did you put your second activity in there?
Does the call work without the putExtra?
Second, what type is collection? Is the source data class annotated with #Serializable? I am not very sure that as Serializable alone will work because there are different Serializables.
https://kotlinlang.org/docs/serialization.html#example-json-serialization
When LogCat does not work, check about filters. If there are none and you see any reactions, then restart Android Studio. There are some Bugs which can stop LogCat from working.

How to create permanent WMI event consumer that displays message?

I am trying to track the occurrence of specified Security events. I want a message to be displayed to the user whenever these events are logged in the Windows Security log. It was recommended that I use a permanent WMI event consumer/watcher to accomplish this but I have never used this before and don't understand how to implement it based on the documentation.
If anyone can explain how I can do this for, as an example, Event 1102, it would be much appreciated.
You can user ORMi to create watcher and get any new event:
WMIWatcher watcher = new WMIWatcher("root\\CimV2", "Select * From __InstanceCreationEvent WHERE TargetInstance ISA 'Win32_NTLogEvent' and TargetInstance.LogFile='Application'");
watcher.WMIEventArrived += Watcher_WMIEventArrived;
private static void Watcher_WMIEventArrived(object sender, WMIEventArgs e)
{
//HANDLE EVENTS
}
Be sure to check out if the query returns the events you are expecting.

Lightswitch task on a background thread

I have a lightswitch app which sends an email when a new job is added. I added a sendemail helper class which is called when the record is inserted.
This works however the interface hangs on save waiting for the email to be sent.
I would like to perform this asychronously so that the user can go on and do his thing while the email sends in the background.
I've tried creating a new thread in the inserted part, hoping it would spin off the thread and then return to the user, but it doesn't work, it is still waiting for the thread to finish.
Thread caseemail = new Thread(new ParameterizedThreadStart(newSendmail.generateCaseEmail));
string[] paramsToPass = new String[] { entity.ProjectNumber, entity.CreatedBy, entity.TheProjectClientManagerFixed, entity.ProjectName };
caseemail.Start(paramsToPass);
How should I be doing this?
So in the end this code works, the errors were from problems access dataworkspaces from the other thread, which is obviously not allowed.
I will leave this here as an example of how to spin off a task into the background from lightswitch, leaving the interface responsive while lightswitch goes away and does something else.

JavaFX2: Can I pause a background Task / Service?

I am trying to set up a background service that would perform bulk loading of transaction data from a csv file. This background service would be initiated from a menu item action mapped to a method in the controller/presenter class.
Ever so often, some data turns up in the csv file for which no master data can be found in the database, this would normally cause the upload to choke and fail.
On such occasions, I would like to be able to have the background service pause its processing and invoke a dialog from a presenter class to take in user input. The user input would be used to add a master row in the database, after which the background service should resume from where it had left off (not from the beginning of the csv file, but from the row which caused the error).
Is this possible to achieve in JavaFX, perhaps with the javafx.concurrent API? How would I go about doing this?
Solution
When your background process encounters a situation where it requires a user to be prompted for input, use FutureTask executed in Platform.runLater to showAndWait the dialog prompt on the JavaFX application thread. In the background process use futureTask.get to pause the background process until the user has input the necessary values which will allow the process to continue.
Sample Code Snippet
Here is the essence of code for this approach which can be placed inside the call method of your background process:
String nextText = readLineFromSource();
if ("MISSING".equals(nextText)) {
updateMessage("Prompting for missing text");
FutureTask<String> futureTask = new FutureTask(
new MissingTextPrompt()
);
Platform.runLater(futureTask);
nextText = futureTask.get();
}
...
class MissingTextPrompt implements Callable<String> {
private TextField textField;
#Override public String call() throws Exception {
final Stage dialog = new Stage();
dialog.setScene(createDialogScene());
dialog.showAndWait();
return textField.getText();
}
...
}
Sample Application
I created a small, complete sample application to demonstrate this approach.
The output of the sample application is:
Sample Output Explanation
Lines read without missing values are just plain brown.
Lines with a prompt value entered have a pale green background.
Fourteen lines have been read, the background task has already paused once at the 6th line which was missing a value. The user was prompted for the missing value (to which the user entered xyzzy), then the process continued until line 14 which is also missing and the background task is again paused and another prompt dialog is being displayed.

Auto Refresh the List in Sencha Touch Application

I am developing an simple Chat Application, I can view the updated data when I click on the REFRESH Button, BUT can I refresh the data at a regular interval from the Server (as my Chat is getting Stored in the Database remotely)
Thanks in advance.
Use DelayedTask class of Sencha Touch:
//create the delayed task instance with our callback
var task = Ext.create('Ext.util.DelayedTask', function() {
//load the list's store here. The list will be automatically updated
listComp.getStore().load(); // Assuming your list component is "listComp"
listComp.refresh();
// The task will be called after each 10000 ms
task.delay(10000);
}, this);
//The function will start after 0 milliseconds
//so we want to start instantly at first
task.delay(0);
//to stop the task, just call the cancel method
//task.cancel();
This should work for your case.
You just need to call your refresh() function at a regular right ?
So you just need to add a setTimeout("refresh()", 1000); at the end of your refresh() function. Then you just need to call it when your app startup.
Hope this helps
Maybe you'll be interested in socket server connection in your chat application. Server will notify your clients whenever server data changes. Take a look at http://socket.io/
please look at this.
It will call the function for every (time you set).
http://www.sencha.com/forum/showthread.php?194202-Autorefresh-List-with-DelayedTask

Resources