I'm trying to set a background from ActivityB to AcitivtyA using getInstance but it only show me the Toast message, whenever turn back to ActivityA, there's no change.
I have this in my ActivityA:
private static MainActivity activityA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityA = this;
}
public static MainActivity getInstance(){
return activityA;
}
public void setLeavesBackground() {
FrameLayout mainFrameLyt = (FrameLayout) findViewById(R.id.mainFrameLayout);
mainFrameLyt.setBackgroundColor(Color.parseColor("#00FF00"));
Toast.makeText(getBaseContext(), "New style applied", Toast.LENGTH_SHORT).show();
}
And this in my ActivityB:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_style);
ImageButton leavesBtn = (ImageButton) findViewById(R.id.leavesBtn);
leavesBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.getInstance().setLeavesBackground(); //call myFunction using activityA
}
});
}
What should I do?
You approach to get an Instance of another Activity and modify it from within another activity is wrong:
public static MainActivity getInstance(){
return activityA;
}
You have to obey the Activity lifecycle
A way how you could set another activities background is by creating a shared preferences value for the background color and use it in the onCreate method of your Activity.ñ
Related
I want to finish an activity by an image, and here is what I did:
there's an ImageView in the layout [activity_login.xml] .
/**
* the activity that I want to destroy
*/
public class LoginActivity extends Activity {
public static LoginActivity activityInstance;
private ImageView imgBtnBack;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// for closing this activity by an another class
activityInstance = this;
// add event listener
imgBtnBack = (ImageView) findViewById(R.id.img_btn_back);
imgBtnBack.setOnClickListener(new LoginActivityListener());
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
/**
* the event handler of LoginActivity
*/
public class LoginActivityListener implements View.OnClickListener{
private Context context;
#Override
public void onClick(View view) {
context = view.getContext();
int id = view.getId();
switch (id) {
case R.id.img_btn_back: // close the activity by an image
LoginActivity.activityInstance.finish();
default:
break;
}
}
}
And I don't know if it was good by the way I did.
can anyone find and tell me a better way to make this work.
Add below code in your onCreate method
imgBtnBack = (ImageView) findViewById(R.id.img_btn_back);
imgBtnBack .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
I have activity flow
Activity 1-> Activity 2->Activity 3
If the user performs certain action in Activity 3, shared preferences boolean variable "performed" stores true. Now if the user presses back button to load Activity 2, it doesn't (i used finish()) and loads Activity 1. This is fine and as per the need.
What I want now is, when Activity 1 restarts this way, based on the value of the shared preferences boolean variable "performed" value true the button in Activity should be hidden.
I am using,
if (preferences.getBoolean("performed",false)){
button.setVisibility(View.Gone);
}
I have written this code in onRestart method of the Activity 1.
What am I missing?
The button does not hides.
I have done it in following way
- wrote a singalton class has variable IsVisited.
- check if it is visited in onResume() method
- if visited then set visibility of the button gone
Main Activity
Controller c;
Button bu ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bu = (Button)findViewById(R.id.button1);
c = Controller.getController();
bu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
c.setIsVisited(true);
Intent i = new Intent(MainActivity.this, mainactivity2.class);
startActivity(i);
}
});
}
protected void onResume() {
super.onResume();
if(c.getIsVisited())
{
bu.setVisibility(View.GONE);
}
Another Activity
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Controller c = Controller.getController();
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
Controller Class (Singleton Class)
public class Controller {
private static final Controller controller = new Controller();
private Boolean isVisited = false;
private Controller()
{
}
public static Controller getController()
{
return controller;
}
public Boolean getIsVisited() {
return isVisited;
}
public void setIsVisited(Boolean isVisited) {
this.isVisited = isVisited;
}}
![enter image description here][1]
I have three classActivity created. One is super class and other sub class and third is HomeActivity.
Code for super class is :
public class MyActivity extends Activity {
Button btnHome = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onHomeClick(View view) {
String LOG_TAG = "Akshar";
System.out.println("Hello11111");
btnHome = (Button) view;
Log.v(LOG_TAG, "index=" + btnHome);
}
}
and my subclass code is :
public class ChooseIsland extends MyActivity {
Button btn_home = null;
MyActivity ob1 = new MyActivity();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chooseiland);
addListenerOnButton();
}
private void addListenerOnButton() {
// TODO Auto-generated method stub
ob1.onHomeClick(btn_home);
}
}
Now I want to go on Home page when click so when I write ?
Intent intent = new Intent(this, HomeActivity.class);
There is no close operation as such in android. You should make sure you do not save anything in stack so whenever you are traversing from one activity to other, make sure you use intent flags to clear history or top of stack and then call finish.
finish() will close the current activity and the previous activity will come to foreground.
Generally to we write finish() method to close activity.
Actually i want to call a function of one activity from another activity..i write a simple code for that but its not working..and show runtime error..please check the code...if there any mistake..
code for activity1:
public class Activity1 extends Activity2
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
call();
}
public void call()
{
showToast("Helloo");
}
}
code for activity2:
public class Activity2 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showToast(String s)
{
EditText t=(EditText)findViewById(R.id.editText1);
t.setText(s);
}
}
Your problem is that you're calling findViewById on a view that doesn't exist.
Activity1 is extending Activity2.
You call the super.onCreate in Activity1 which calls the onCreate in Activity2 which calls setContentView() for R.layout.main.
I'm guessing your text R.id.editText1 is in the main layout.
When Activity1 returns from the call to super.onCreate it immediately resets the content layout to main2.
The edit text box that you are trying to edit no longer exists. findViewById can not find it because the layout is not active. Thus it crashes.
To fix it try this:
public class Activity1 extends Activity2
{
private EditText et;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
et = (EditText) findViewById(R.id.editText2);
call();
}
public void call()
{
showToast("Helloo", et);
}
}
Where R.id.editText2 is an edit text box in the layout main2.
In Activity2:
public class Activity2 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showToast(String s, EditText t)
{
t.setText(s);
}
}
First, this is a bad design principle since only one Activity is active at a time. You can make a method static and then you can cross call them but at that point it should be in some sort of common util class.
The simplest way is to declare your showToast() method as public static, this way you can call it without having an instance of Activity2.
if you put it in as static you should declare it on your main activity
While the application is running, I press the HOME button to close the application. When I start the application again, it resumes on the page displayed prior to clicking on HOME. I want the application to start with the initial display instead. I have used finish() to finish the activity but it is not working. Any suggestions?
Most likely you have several instances of the same activity. To resolve this kind of issues create your own parent Activity class e.g. MyRootActivity which will hold static list of all of available/alive activities:
public class MyRootActivity extends Activity
{
private static final String TAG=MyRootActivity.class.getName();
private static ArrayList<Activity> activities=new ArrayList<Activity>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
activities.add(this);
}
#Override
public void onDestroy()
{
super.onDestroy();
activities.remove(this);
}
public static void finishAll()
{
for(Activity activity:activities)
activity.finish();
}
}
For that all of your activities need to be children of MyRootActivity.
Then when you are about to sure that you're closing your application - just call MyRootActivity.finishAll();
Create a static Activity object which activity finish on other activity and assign activity in this i.e you can can add more activities
public class demoActivity extends AppCompatActivity {
public static Activity self_intent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_activity);
selfintent=this;
}
//Other functions--------------
}
do same for other activities
on other
public class finishingActivity extends AppCompatActivity {
public Button activityCloseBtn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.finishing_activity);
activityCloseBtn= (Button) view.findViewById(R.id.activity_close_btn);
activityCloseBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
demoActivity.selfintent.finish(); //for finish demoActivityactivity
//for other activities Activity.selfintent.finish();
finish(); //for finish current activity
}
});
try calling super.onPause() first and later call finish() inside your onPause() stub