New Activity / strange behaviour ..

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

New Activity / strange behaviour ..

Postby S3bast1an » Mon Feb 15, 2010 10:22 am

Hey,

I have two activities (one main, one for the about-screen).
In my main-window-menu I start the About-class like that:

Code: Select all
Intent myIntent = new Intent(v.getContext(), About.class);
startActivity(myIntent);


The about-layout has a button.
When I FIRST click the button the title of the window changes to "About" (before it still shows the title from the main-window; "About" is the correct name of the activity defined in the manifest.xml) and nothing else happens. On the SECOND click it jumps to my main-window, but reseting all my textfields.


Code: Select all
@Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.about);
      final Button button = (Button) findViewById(R.id.ButtonBack);
      button.setOnClickListener(new Button.OnClickListener() {
         public void onClick(View v) {
            Intent myIntent = new Intent(v.getContext(), Main.class);
               startActivity(myIntent);
         }
      });
   }
}


Any idea about the strange behaviour of the button, not working on first click?

Is it correct to start an new activity like that and jump back like that, or is there an other/better way?

In the About-window, I still can use the main-menu (what is not wished), do I have to unregister the menu in any way? And reregister later?

Thanks a lot.
I wonder how complicated it seems to make an about screen - the rest of the app works quite fine. :)

Greets
S.
S3bast1an
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sat May 02, 2009 4:27 pm

Top

Postby pmws » Mon Feb 15, 2010 11:04 am

Since your main Activity is stilll running when you go to the about view, you dont need to restart it via an Intent.
Just close it when you hit the button by calling finish().
That should keep the values of your Textfields.

hope this helps
pmws
Developer
Developer
 
Posts: 37
Joined: Thu Sep 24, 2009 12:23 pm

Postby S3bast1an » Mon Feb 15, 2010 11:29 am

pmws wrote:Since your main Activity is stilll running when you go to the about view, you dont need to restart it via an Intent.
Just close it when you hit the button by calling finish().
That should keep the values of your Textfields.

hope this helps


mhm .. when simply do a "finish()", the about screen will not vanish, only the title changes everytime I press the button (from the name of the fist activity to the name of the about-activity and back).

Or is there something wrong in calling the about-activity?

Does finishing the about-activity calls the OnCreate() of the main-activity? Obviously not!

Greets
S.
S3bast1an
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sat May 02, 2009 4:27 pm

Postby pmws » Mon Feb 15, 2010 11:32 am

...on a second thougth the best way to do the about screen might be:

In your main class:
When the about Button is clicked:
setContentView(R.layout.about);

..and when the "close Button" in about is clicked back to :
setContentView(R.layout.main);
pmws
Developer
Developer
 
Posts: 37
Joined: Thu Sep 24, 2009 12:23 pm

Postby S3bast1an » Mon Feb 15, 2010 11:54 am

pmws wrote:...on a second thougth the best way to do the about screen might be:

In your main class:
When the about Button is clicked:
setContentView(R.layout.about);

..and when the "close Button" in about is clicked back to :
setContentView(R.layout.main);


But after calling setContentView(R.layout.about) it seem my OnCreate in about-class is not called an I cannot set my onclicklistener - so my button is not working?

And the menu is still working - on the wrong screen. (I dont want the menu to be active on the aboutscreen)

Greets
S.
S3bast1an
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sat May 02, 2009 4:27 pm

Postby pmws » Mon Feb 15, 2010 12:01 pm

S3bast1an wrote:
But after calling setContentView(R.layout.about) it seem my OnCreate in about-class is not called an I cannot set my onclicklistener - so my button is not working?

And the menu is still working - on the wrong screen. (I dont want the menu to be active on the aboutscreen)

Greets
S.


You can get a Handle to the button from the about layout in your main class. (Just make sure it has a diffrent ID then the Buttons there.

As for blocking the menu, you could override the onKeyDown method and check if about is opend or not before you call the menu.

EDIT : the Handle must be made after the layout is switched it seems. And after you switch back the listeners must be reassinged. So its actually a bit more complicated than i thought. Sorry bout that.
I still dont see why the first aproch using diffrent Activities with intends is not working. It works on my apps.

EDIT2 : Also im afraid your textfieds will also be reset using this method. Again sorry for putting you on the right track.
pmws
Developer
Developer
 
Posts: 37
Joined: Thu Sep 24, 2009 12:23 pm

Top

Postby S3bast1an » Mon Feb 15, 2010 2:21 pm

pmws wrote:As for blocking the menu, you could override the onKeyDown method and check if about is opened or not before you call the menu.


Thank you,
but how do I check if "about" is opened?
And if it is opened ... How do I override the Menulistener?

I tried to use menu.clear(), but at the Menulistener I only get the item not the menu! Is there a function that gives me the menu for a givenitem?

Greets
S., who still wonders how complicated things could get if you only want to show an Aboutscreen. ;)

PS:
To recover my form .. is savedInstanceState the right thing?! Or is there a better/preferd way?
S3bast1an
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sat May 02, 2009 4:27 pm

Postby S3bast1an » Mon Feb 15, 2010 4:54 pm

S3bast1an wrote:To recover my form .. is savedInstanceState the right thing?! Or is there a better/preferd way?


Mhm .. is there ANY function called after I come back to my main-screen with setContentView(R.layout.main)?
At the moment, it comes back from the "About"-screen an my listeners are gone and no idea how to revive?
Also I don't know how to restore the data in the EditText, that is gone after that.

Honestly, I dont understand why OnCreate/OnResume isn't called? The view is gone and comes back .. not a case for OnCreate or OnResume?

Greets
S.
sorry for so many questions ...
S3bast1an
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sat May 02, 2009 4:27 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests