I'm trying a method to reduce the number of total activities used in an application by switching layouts using the same activity. What I am doing is -
/* Class A is the actual activity */
public class A extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but=(Button)findViewById(R.id.button1);
but.setOnClickListener(new View.OnClickListener(){
/*When the button is pressed, create an object of class B to switch layout*/
public void onClick(View arg0) {
B b=new B(A.this);
}
});
}
}
/*Classs B handles some operation*/
public class B{
Activity a;
/*Set the new layout inside the constructor or call some other function to do that*/
B(Activity act){
a=act;
a.setContentView(R.layout.newlayout);
}
So, the Class B would switch the layout, do some operation. I would like to know whether this method is good practice, and also if there are other methods to do the same. Thanks
Honestly i don't think is a good practice because in this way to keep alive several classes in your stack because they are referenced by the main activiy class that is always running so it could be a waste of resources.
Managing several activities that handle specific task is much better in my opinion for 2 reason:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.