How to restart the onCreate after coming back from another activity?
Using below to
val intent = Intent(this, WritePage::class.java)
startActivity(intent)
When im finished
finish()
So i’m back to the previous page. How can i restart this page from onCreate?
This is the offical documentation of the lifecycle of an app. If you finish an activity you can only restart that activity by creating a new instance of it. If you want to reuse it you should only pause that activty so you can easily resume that same instance of the activity (as Chris Shaw already mentioned).
Related
I try to program an kids-launcher.
I search for my problem three days but I do not find a solution (I am a beginner in android/kotlin)
I want to start an app and after a while (when the time for the kids is elapsed) I want to come back to my launcher
I wrote the function
fun openApp(context: Context, packageName: String?) {
context.startActivity(context.packageManager.getLaunchIntentForPackage(packageName!!))
}
I started an app and after a while (when the time for the kids is elapsed) I want to come back to my launcher
openApp(this, packageName)
timer.schedule(10000) {
openApp(this, "PackageNameMyLauncher")
}
But it does not work.
when I write "openApp(applicationContext, packageName)" instead, than it works a little bit. But when the app is closed and I press the home button than it won´t work.
I think I didn´t understand some fundamental at the moment.
In the thread there is no context
I also tried to run a pendingIntent with an alarmManager
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
val mPendingIntent = PendingIntent.getActivity(
this,
123456, //PendingIntentId,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
)
val mgr = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, mPendingIntent)
System.exit(0) //finish()
but that is also not working
I know it is not an answer, but when I was a beginner what I lacked was some kind of upper-level design ideas
I don't think Android provides the ability to schedule app opening without user's intention. And also your timer will be destroyed when you will remove application from processes.
I think your best bet will be to design app that way, that it will be unavailable till some time will elapse. You can save the last timestamp when the app became unavailable on the backend or in shared preferences and later on you can compare it with current timestamp, which you will get each time you open the app. If the
currentTimeStamp - savedTimeStamp >= timeLimit you will make further logic of your app available, otherwise you will display some kind of a message.
I have changed over to Google billing client 4.0 library. Since then when purchase is completed, my ui elements updates does not work.
I was trying to check if they are not in UThread anymore or something, but it is not working properly still.
billingClient!!.acknowledgePurchase(
acknowledgePurchaseParams
) { billingResult ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
saveSubscription(purchase.purchaseToken, sku)
mainAct?.runOnUiThread {
Toast.makeText(context , "Vásárlás sikeres", Toast.LENGTH_SHORT)
.show()
delegate?.purchaseSuccess(
actProduct,
prevProduct,
purchase.purchaseToken
)
}
}
}
When the acknowledge is done, my UI elements should be updated. There is a menu item should be enabled and in the purchase page a textfield should show that the item is purchased. The code is running, but the UI elements are not updated.
Once I move to a different fragment, the menu item turns to enabled, so it seems to me that somehow the UI is not refreshed.
In the code mainAct is the context to the MainActivity.
Any idea?
The code was running properly on Billing library 3.0.3 and runOnUIThread was not needed at all.
Still getting weird behaviour with this library. I was setting up the billing at the same time as my users log in to the app and go from the login page to the first fragment. This fragment has a recyclerview and this recycler view was filled with data from the web. The recycler view was randomly not showing the elements, even if they were loaded and the adapter was properly updated.
One you moved to another fragment and back the recycler view was showing the data properly.
As I said, the first time call when the billing server is approached the UIThread is messed up and even if data labels or menus are updated, the updates were not showed.
So something is definitely wrong with this library.
Any idea?
This behaviour is a bit weird. The documentation is saying the most of the billing library functions can be run from any thread. Only few needs to be run on UIThread. AcknowlegdePurchase should be running on AnyThread.
I have put the Toasts into runOnUIThread and the inside the delegate functions all commands I have also put inside a runOnUIThread closure.
Now it works as on billing library 3.0.3. But the documentation was not telling anything about any of these changes, so it is really unclear, why the behaviour has changed that much between the two libraries.
I'm setting an activity, Receiver, as the content intent for a notification.
Intent clickIntent = new Intent(context, Receiver.class);
mBuilder.setContentIntent(PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT));
Inside Receiver Activity, I'm starting activities which are intended to be opened using TaskStackBuilder in the following way.
Intent intent = new Intent(this, Class.forName(className));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
TaskStackBuilder.create(this).addParentStack(Class.forName(className)).addNextIntent(intent).startActivities();
When the app is in the background and a notification click happens, it resumes the ParentActivity. Especially when device goes to idle and comes back. Any help? I'm cracking my head on this.
For an Android app, you should also declare android:launchMode in your androidManifest.xml file.
As discussed in the Android documentation:
An instruction on how the activity should be launched. There are four modes that work in conjunction with activity flags (FLAG_ACTIVITY_* constants) in Intent objects to determine what should happen when the activity is called upon to handle an intent.
They are:
"standard"
"singleTop"
"singleTask"
"singleInstance"
The default mode is "standard".
Solution given in this SO post - resuming an activity from a notification might also help.
I have a home activity ActivityA, which has button that creates an intent, sets up some Extras and calls ActivityB. This in turn calls ActivityC , and in turn calls ActivityD.
I don't want to have to write a test case that opens ActivityA and proceeds to drill through 4 activities to get to the one I want to test. How can I set up the Extras required by ActivityD when it launches to prevent errors in my code. For example before I call the intent that launches activityD it sets up an Extra which ActivityD then uses.
Thanks
Robotium gives you an option to launch an activity with a given intent, you will need to determine the intent that you need in order to launch the activity correctly. If you look in logcat it might give you the details that you need to launch the activity correctly. If you are unsure what to look for post the logcat logs of the time between clicking the element on Activity C and Activity D launching and i can try to look for you.
I develop an eclipse plugin and I added an eclipse progress view to it. There is an stop button on the progress view and I want to create some kind of listener to handle the events of the cancel button, but I don't know how I can do it. I know the monitor has an isCanceled() method, but I have to create come kind of listener to listen when user clicks the stop button, while the plugin works. Please give me some advice. Thanks
What you have to do here is check the isCanceled method often while you are on your long-running code. If isCanceled returns true, you return from your code properly.
The progress view normally monitors Job objects; you extend org.eclipse.core.runtime.jobs.Job and implement run() to define a Job. That class has a canceling() method; you can override canceling() and set a flag to true. Then in your run() method, you just loop until that flag becomes true (or the work is done), at which point you cleanup and return.