ntgCleaner
ntgCleaner

Reputation: 5985

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.

Upvotes: 1

Views: 1596

Answers (2)

Gaurav Balbhadra
Gaurav Balbhadra

Reputation: 164

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);

Upvotes: 1

mawalker
mawalker

Reputation: 2070

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

Upvotes: 0

Related Questions