overloading onResume() - java

How to overload onResume() to work the correct way? I want to come back from activity to MainActivity where I want to have the same state as after app start. I wanted to use recreate() but it looped or some sort of that.
My code:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
recreate();
}

implement onSaveInstanceState(Bundle save) and onRestoreInstanceState(Bundle restore) to save and restore the state. See the documentation on this.

I guess, when you press back button, you want to refresh the previous activity.
It is pretty simple, you can just put your method in onResume(), and it should do the trick.

Ok Try this out, it is working for me.
public class main extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//add any buttons or anything you have here.
doMainWork(); \\lets just say you have this method, which contains the main code of the layout.
}
protected void onResume(){
super.onResume();
doMainWork();
}
public void doMainWork(){
\\Put all your working code here. And this should work it out man.
}

Related

Calling a non static method from a in a static context

im sorry for the long question, but i could really use the help
so I've been trying to make a camera app for this school project that i have. i'm really new to coding in general, and i don't really know much about Java. i decided to use the CameraKit library by Furgle to help me with this. they say all i have to do is include
protected void onResume() {
super.onResume();
CameraView.start();
and
#Override
protected void onPause () {
super.onPause();
CameraView.stop();
}
i should be able to start and stop the camera preview im trying to create.
however, when i added this code to my main activity, i got the following:
non static method 'stop()' / 'start()' cannot be referenced from a static context
I've tried a few things like trying to create an object of the class and calling the method from that (i'm not completely sure if i said that right or not)
#Override
protected void onResume() {
super.onResume();
CameraView main = new CameraView()
main.start();
when i try to run that, i get:
cannot resolve constructor CameraView()
I also tried to create instances of the class called "CameraView" which is where the method "start();" and "stop();" are. sadly i have not been able to get anywhere with that.
the point is i tried everything that i could understand but any help would be greatly appreciated.
after looking into the code for the library, i saw that neither the start method or the stop method within the CameraView class are declared "static". so i really don't see where the problem is coming from and how to overcome it
Assuming the tutorial you're following is this one https://github.com/gogopop/CameraKit-Android#usage ...
When they say that "all you have to do" is add this code:
#Override
protected void onResume() {
super.onResume();
cameraView.start();
}
#Override
protected void onPause() {
cameraView.stop();
super.onPause();
}
They're speaking to more-experienced developers. The part they're leaving out is where does cameraView come from?
Well, the first step is to include a <CameraView> in your layout. But even after that, you need to find it and assign it to a cameraView variable. So really, you need all this:
public class MainActivity extends AppCompatActivity {
private CameraView cameraView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // `activity_main.xml` must have a `<CameraView>` tag with id `camera`
cameraView = (CameraView) findViewById(R.id.camera);
}
#Override
protected void onResume() {
super.onResume();
cameraView.start();
}
#Override
protected void onPause() {
cameraView.stop();
super.onPause();
}
}

Android use two classes for a single layout

I'm trying to separate some of my java in a few different files.
I have my main class:
public class StartPage extends Activity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_page);
}
...
}
And then I have this other class that I'd like to run on the same layout:
public class part_settings_session extends Activity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_page);
Toast.makeText(getApplicationContext(), "This is from settings", Toast.LENGTH_SHORT).show();
}
...
}
But I'm not able to see that Toast happen anywhere or at any time. Is there a way to make both of these classes work in two separate files? This is to organize scripts for my own sake.
Two Activities can not be visible at same time and here in your code you have defined two Activities with same layout. Your code is fine but to see both activities working, you have to manually start next activity. Below code will help you. This code will start Next Activity 3 seconds after loading First Activity.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(StartPage.this, NextPage.class));
finish();
}
}, 3000);
in your onCreate() for the 2nd class, put a Log.d("part_settings_session", "onCreate"); and see if the onCreate ever gets called in the first place. (Since they are using the same layout, it might be difficult to see if you are 'actually' creating an instance of THIS class.
My guess is that you might not even be creating an instance of the part_settings_session class. And without logging it is pretty hard to tell that.
Here is a nice Activity base class that will log all life-cycle events for you
https://github.com/douglascraigschmidt/CS282/blob/c5cf5c4808ea082988ab87c00eeade4837eac967/assignments/assignment1/src/edu/vandy/common/LifecycleLoggingActivity.java

Finish application from parent class

Have some problem with android finish() methods.
I have one parent-class activity. Lets call it ParentActivity. All other activities in my project extends ParentActivity. Each time on ParentActivity.onCreate there are some statement, and I want to stop activity from executing if it fails. But when I call finish() in parent, I cant stop executing onCreate method on its child. Something like that:
public class ParentActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!someStatement) finish();
}
public class Test extends ParentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("TAG", "I dont want this code!");
}
}
Surely, I can just verify in parent activity its status each time, but I dont think its a good idea.
public class Test extends RexActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFinishing()) return; /// It works - but it bad :(((
Log.d("TAG", "I dont want this code!");
}
}
Can I somehow stop executing onCreate method on child activity from its parent? Many thanks for any help!
I'm not sure if I got your question right. As you have some grammatical issues.
The onCreate statements are always executed. You can either have a Boolean in ParentActivity to stop the code from executing in ChildActivity#onCreate().
You can try making your onCreate() code more modular by dividing it into functions so that it's not called.
Let me know what works for you.
Best option is to Use finish() in your splash screen just before you create your second activity,

Dismiss dialogue after configuration is changed

I'm currently writing on a Bluetooth application only allowing the user to work with it in the portait mode. However, there are some other reasons causing confguration changes forcing me to show the user that the Bluetooth connection has been lost.
While connecting to a device I show a dialogue prompting a password. If a configuration change occurs while this dialogue is visible I want to dismiss it. Therefore I call a helper method in onSaveInstanceState, onDestroy as well as onCreate (if the saved instance state is not null).
#Override
public void onCreate(Bundle savedInstanceState)
{
if (savedInstanceState != null)
{
this.tryDismiss();
}
super.onCreate(savedInstanceState);
}
#Override
public void onSaveInstanceState(Bundle outState)
{
System.out.println("Saving instance state!");
this.tryDismiss();
super.onSaveInstanceState(outState);
}
#Override
public void onDestroy()
{
System.out.println("Destroying dialogue!");
this.tryDismiss();
super.onDestroy();
}
public void tryDismiss()
{
try
{
System.out.println("DIE BART, DIE!");
this.dismissAllowingStateLoss();
}
catch (Exception closingException)
{
}
}
However, the dialogue is still displayed to the user. I have read a similar questions, but couldn't figure out a solution to this issue.
I appreciate any help. Thanks in advance!
You can easily dismiss() method
http://developer.android.com/reference/android/app/Dialog.html
Using a strong reference in the activity displaying the dialogue and invoking the dismiss() method in the activitie's onSaveInstanceStatemethod fixed this issue for me. I know, it's not the most optimal solution, but it works.
#Override
protected void onSaveInstanceState(Bundle persistantState)
{
if (this.connectionPasswordDialogue != null)
{
this.connectionPasswordDialogue.dismiss();
}
}
With strong reference I mean a private attribute used internally by the activity:
private DialogueUserModePassword connectionPasswordDialogue;

Android onCreate() not working

I'm having a strange problem to do with stopping my android app. On my phone I have a home button and a back button, now when I go into my app after pressing the home button, the program loads data from the internet as expected, but when I go into my app after pressing the back button, the data doesn't load. I've debugged it to an extent, and have found out that the only difference is that the back button calls the onCreate() method. I'm quite confused to why this is is happening.
Here's some of my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("DAP", "Created");
setContentView(R.layout.activity_ltc);
getActionBar().setTitle("LTC Charts");
getActionBar().setLogo(
getResources().getDrawable(R.drawable.new_litecoin_logo_large));
TextView textView = (TextView) findViewById(R.id.ltcdata);
textView.setText("Loading data...");
TimerTask timer = new TimerTask() {
#Override
public void run() {
parseJSON();
}
};
Timer time = new Timer();
time.schedule(timer, 500, 85);
}
"when I go into my app after pressing the back button, the data doesn't load."
If you have already launched your app, the Activity will be paused (and onPause is called) when you navigate away from it. When you navigate back to the app, the same activity instance is resumed (and onResume is called).
See http://developer.android.com/training/basics/activity-lifecycle/index.html
This is because your activity hasnt been destroyed. What you should do is put something into the onresume of your activity to get the data again when you come back to the activity. If you want the data to be destroyed for sure and the user never leaves the activity you can destroy everything in onpause.
As others said, override onResume().
A common pattern would be to extract common initialisation code into a method, and call it from where needed:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
#Override
protected void onResume(){
super.onResume();
init();
}
private void init(){
setContentView(R.layout.activity_ltc);
getActionBar().setTitle("LTC Charts");
// ....
}
This could be because your activity is not set correctly at AndroidManifest.xml .
Make sure the activity name is the right one. If you inserted your activity into some package include that name as well . for example I have my activitu which is called "SettingsActivity" (usually the default is MainActivity) set in a package called Activities :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<activity android:name=".Activities.SettingsActivity">
...
Use the on resume function below.
#Override
public void onResume(){
//will be executed onResume
}

Categories