How do I call one method of an activity from another class
This is a part of my activity class,
public class StatusActivity extends AppCompatActivity {
private boolean cFlag = false;
public boolean getFlag() return cFlag;
public void setFlag(boolean cFlag)
this.cFlag = cFlag;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
android.R.id.text1, messages);
ListView listView = findViewById(android.R.id.list);
listView.setAdapter(adapter);
adapters.add(adapter);
Button btn = findViewById(R.id.btnCustomerCheckIn);
btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
setFlag(true);
cFlag = getFlag();
Intent intent = new Intent(StatusActivity.this, MainActivity.class);
Toast.makeText(StatusActivity.this, "customer checked in",
Toast.LENGTH_LONG).show();
startActivity(intent);
);
this is a part of another class named as position
public class Position
StatusActivity statusactivity = new StatusActivity();
public boolean ccflag = statusactivity.getFlag();
statusactivity.setFlag(false);
when i am calling
statusactivity.setFlag(false);
it is showing an error. couldn't recognize that what is the error that i am getting. but
statusactivity.getFlag();
is working properly. any help is appreciated
java android
|
show 3 more comments
This is a part of my activity class,
public class StatusActivity extends AppCompatActivity {
private boolean cFlag = false;
public boolean getFlag() return cFlag;
public void setFlag(boolean cFlag)
this.cFlag = cFlag;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
android.R.id.text1, messages);
ListView listView = findViewById(android.R.id.list);
listView.setAdapter(adapter);
adapters.add(adapter);
Button btn = findViewById(R.id.btnCustomerCheckIn);
btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
setFlag(true);
cFlag = getFlag();
Intent intent = new Intent(StatusActivity.this, MainActivity.class);
Toast.makeText(StatusActivity.this, "customer checked in",
Toast.LENGTH_LONG).show();
startActivity(intent);
);
this is a part of another class named as position
public class Position
StatusActivity statusactivity = new StatusActivity();
public boolean ccflag = statusactivity.getFlag();
statusactivity.setFlag(false);
when i am calling
statusactivity.setFlag(false);
it is showing an error. couldn't recognize that what is the error that i am getting. but
statusactivity.getFlag();
is working properly. any help is appreciated
java android
What is the error you are seeing?
– Janwilx72
Nov 14 '18 at 7:22
3
Never instantiate anActivity
yourself. This is the task of the framework, which will make all necessary initializations and call the activitie's life cycle methods. If you need an instance of the activity in your class pass it as a parameter.
– Henry
Nov 14 '18 at 7:26
StatusActivity statusactivity = new StatusActivity();
is just plain wrong, instead of this, you should inject yourPosition
object into that activity and have control over it.
– Jay
Nov 14 '18 at 7:27
We never create an instance of an activity by ourself. Its created by android framework work behind the scene. To call activitie's method from other class you need to pass reference of activity to that class
– Umer Farooq
Nov 14 '18 at 7:34
@Janwilx72 its showing cannot resolve symbol 'setFlag'
– Shafeeq Mohammed
Nov 14 '18 at 8:10
|
show 3 more comments
This is a part of my activity class,
public class StatusActivity extends AppCompatActivity {
private boolean cFlag = false;
public boolean getFlag() return cFlag;
public void setFlag(boolean cFlag)
this.cFlag = cFlag;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
android.R.id.text1, messages);
ListView listView = findViewById(android.R.id.list);
listView.setAdapter(adapter);
adapters.add(adapter);
Button btn = findViewById(R.id.btnCustomerCheckIn);
btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
setFlag(true);
cFlag = getFlag();
Intent intent = new Intent(StatusActivity.this, MainActivity.class);
Toast.makeText(StatusActivity.this, "customer checked in",
Toast.LENGTH_LONG).show();
startActivity(intent);
);
this is a part of another class named as position
public class Position
StatusActivity statusactivity = new StatusActivity();
public boolean ccflag = statusactivity.getFlag();
statusactivity.setFlag(false);
when i am calling
statusactivity.setFlag(false);
it is showing an error. couldn't recognize that what is the error that i am getting. but
statusactivity.getFlag();
is working properly. any help is appreciated
java android
This is a part of my activity class,
public class StatusActivity extends AppCompatActivity {
private boolean cFlag = false;
public boolean getFlag() return cFlag;
public void setFlag(boolean cFlag)
this.cFlag = cFlag;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
android.R.id.text1, messages);
ListView listView = findViewById(android.R.id.list);
listView.setAdapter(adapter);
adapters.add(adapter);
Button btn = findViewById(R.id.btnCustomerCheckIn);
btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
setFlag(true);
cFlag = getFlag();
Intent intent = new Intent(StatusActivity.this, MainActivity.class);
Toast.makeText(StatusActivity.this, "customer checked in",
Toast.LENGTH_LONG).show();
startActivity(intent);
);
this is a part of another class named as position
public class Position
StatusActivity statusactivity = new StatusActivity();
public boolean ccflag = statusactivity.getFlag();
statusactivity.setFlag(false);
when i am calling
statusactivity.setFlag(false);
it is showing an error. couldn't recognize that what is the error that i am getting. but
statusactivity.getFlag();
is working properly. any help is appreciated
java android
java android
edited Nov 14 '18 at 7:56
Utkarsh Srivastava
718
718
asked Nov 14 '18 at 7:19
Shafeeq MohammedShafeeq Mohammed
107
107
What is the error you are seeing?
– Janwilx72
Nov 14 '18 at 7:22
3
Never instantiate anActivity
yourself. This is the task of the framework, which will make all necessary initializations and call the activitie's life cycle methods. If you need an instance of the activity in your class pass it as a parameter.
– Henry
Nov 14 '18 at 7:26
StatusActivity statusactivity = new StatusActivity();
is just plain wrong, instead of this, you should inject yourPosition
object into that activity and have control over it.
– Jay
Nov 14 '18 at 7:27
We never create an instance of an activity by ourself. Its created by android framework work behind the scene. To call activitie's method from other class you need to pass reference of activity to that class
– Umer Farooq
Nov 14 '18 at 7:34
@Janwilx72 its showing cannot resolve symbol 'setFlag'
– Shafeeq Mohammed
Nov 14 '18 at 8:10
|
show 3 more comments
What is the error you are seeing?
– Janwilx72
Nov 14 '18 at 7:22
3
Never instantiate anActivity
yourself. This is the task of the framework, which will make all necessary initializations and call the activitie's life cycle methods. If you need an instance of the activity in your class pass it as a parameter.
– Henry
Nov 14 '18 at 7:26
StatusActivity statusactivity = new StatusActivity();
is just plain wrong, instead of this, you should inject yourPosition
object into that activity and have control over it.
– Jay
Nov 14 '18 at 7:27
We never create an instance of an activity by ourself. Its created by android framework work behind the scene. To call activitie's method from other class you need to pass reference of activity to that class
– Umer Farooq
Nov 14 '18 at 7:34
@Janwilx72 its showing cannot resolve symbol 'setFlag'
– Shafeeq Mohammed
Nov 14 '18 at 8:10
What is the error you are seeing?
– Janwilx72
Nov 14 '18 at 7:22
What is the error you are seeing?
– Janwilx72
Nov 14 '18 at 7:22
3
3
Never instantiate an
Activity
yourself. This is the task of the framework, which will make all necessary initializations and call the activitie's life cycle methods. If you need an instance of the activity in your class pass it as a parameter.– Henry
Nov 14 '18 at 7:26
Never instantiate an
Activity
yourself. This is the task of the framework, which will make all necessary initializations and call the activitie's life cycle methods. If you need an instance of the activity in your class pass it as a parameter.– Henry
Nov 14 '18 at 7:26
StatusActivity statusactivity = new StatusActivity();
is just plain wrong, instead of this, you should inject your Position
object into that activity and have control over it.– Jay
Nov 14 '18 at 7:27
StatusActivity statusactivity = new StatusActivity();
is just plain wrong, instead of this, you should inject your Position
object into that activity and have control over it.– Jay
Nov 14 '18 at 7:27
We never create an instance of an activity by ourself. Its created by android framework work behind the scene. To call activitie's method from other class you need to pass reference of activity to that class
– Umer Farooq
Nov 14 '18 at 7:34
We never create an instance of an activity by ourself. Its created by android framework work behind the scene. To call activitie's method from other class you need to pass reference of activity to that class
– Umer Farooq
Nov 14 '18 at 7:34
@Janwilx72 its showing cannot resolve symbol 'setFlag'
– Shafeeq Mohammed
Nov 14 '18 at 8:10
@Janwilx72 its showing cannot resolve symbol 'setFlag'
– Shafeeq Mohammed
Nov 14 '18 at 8:10
|
show 3 more comments
2 Answers
2
active
oldest
votes
StatusActivity statusactivity = new StatusActivity();
This is totally wrong because you are trying to create a new Instance of activity.
If you want to use "setFlag" method from other activity then you must create a static method inside StatusActivity so you can access using directly StatusActivity.
And If you want to call from any fragment of this activity then please get an instance of this activity by the cast from "getActivity()" to StatusActivity and use that instance for call "setFlag" or "getFlag" method.
You can implement like below in Activity.
private static boolean cFlag = false;
public static boolean getFlag()
return cFlag;
public static void setFlag(boolean cFlag)
StatusActivity.cFlag = cFlag;
and call from position class like below
public class Position
public boolean ccflag = StatusActivity.getFlag();
StatusActivity.setFlag(false);
could you explain the right way with an example,it could have a greet help. thank you
– Shafeeq Mohammed
Nov 14 '18 at 8:31
Check my answer I was edited with an example.
– gunavant patel
Nov 14 '18 at 9:29
StatusActivity.setFlag(false); is showing error(cannot resolve symbol 'setFlag')
– Shafeeq Mohammed
Nov 14 '18 at 10:49
it worked :) thank you
– Shafeeq Mohammed
Nov 14 '18 at 11:16
add a comment |
you can not instantiate Activity class. if you want to call a method from activity, fist you should check that activity already running and not destroyed then by having the context of your class you just cast it like below then use its method
StatusActivity statusactivity= (StatusActivity )context;
statusactivity.setFlag(false);
1
public boolean ccflag = statusactivity.getFlag(); this code is working for me
– Shafeeq Mohammed
Nov 14 '18 at 8:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294953%2fhow-do-i-call-one-method-of-an-activity-from-another-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
StatusActivity statusactivity = new StatusActivity();
This is totally wrong because you are trying to create a new Instance of activity.
If you want to use "setFlag" method from other activity then you must create a static method inside StatusActivity so you can access using directly StatusActivity.
And If you want to call from any fragment of this activity then please get an instance of this activity by the cast from "getActivity()" to StatusActivity and use that instance for call "setFlag" or "getFlag" method.
You can implement like below in Activity.
private static boolean cFlag = false;
public static boolean getFlag()
return cFlag;
public static void setFlag(boolean cFlag)
StatusActivity.cFlag = cFlag;
and call from position class like below
public class Position
public boolean ccflag = StatusActivity.getFlag();
StatusActivity.setFlag(false);
could you explain the right way with an example,it could have a greet help. thank you
– Shafeeq Mohammed
Nov 14 '18 at 8:31
Check my answer I was edited with an example.
– gunavant patel
Nov 14 '18 at 9:29
StatusActivity.setFlag(false); is showing error(cannot resolve symbol 'setFlag')
– Shafeeq Mohammed
Nov 14 '18 at 10:49
it worked :) thank you
– Shafeeq Mohammed
Nov 14 '18 at 11:16
add a comment |
StatusActivity statusactivity = new StatusActivity();
This is totally wrong because you are trying to create a new Instance of activity.
If you want to use "setFlag" method from other activity then you must create a static method inside StatusActivity so you can access using directly StatusActivity.
And If you want to call from any fragment of this activity then please get an instance of this activity by the cast from "getActivity()" to StatusActivity and use that instance for call "setFlag" or "getFlag" method.
You can implement like below in Activity.
private static boolean cFlag = false;
public static boolean getFlag()
return cFlag;
public static void setFlag(boolean cFlag)
StatusActivity.cFlag = cFlag;
and call from position class like below
public class Position
public boolean ccflag = StatusActivity.getFlag();
StatusActivity.setFlag(false);
could you explain the right way with an example,it could have a greet help. thank you
– Shafeeq Mohammed
Nov 14 '18 at 8:31
Check my answer I was edited with an example.
– gunavant patel
Nov 14 '18 at 9:29
StatusActivity.setFlag(false); is showing error(cannot resolve symbol 'setFlag')
– Shafeeq Mohammed
Nov 14 '18 at 10:49
it worked :) thank you
– Shafeeq Mohammed
Nov 14 '18 at 11:16
add a comment |
StatusActivity statusactivity = new StatusActivity();
This is totally wrong because you are trying to create a new Instance of activity.
If you want to use "setFlag" method from other activity then you must create a static method inside StatusActivity so you can access using directly StatusActivity.
And If you want to call from any fragment of this activity then please get an instance of this activity by the cast from "getActivity()" to StatusActivity and use that instance for call "setFlag" or "getFlag" method.
You can implement like below in Activity.
private static boolean cFlag = false;
public static boolean getFlag()
return cFlag;
public static void setFlag(boolean cFlag)
StatusActivity.cFlag = cFlag;
and call from position class like below
public class Position
public boolean ccflag = StatusActivity.getFlag();
StatusActivity.setFlag(false);
StatusActivity statusactivity = new StatusActivity();
This is totally wrong because you are trying to create a new Instance of activity.
If you want to use "setFlag" method from other activity then you must create a static method inside StatusActivity so you can access using directly StatusActivity.
And If you want to call from any fragment of this activity then please get an instance of this activity by the cast from "getActivity()" to StatusActivity and use that instance for call "setFlag" or "getFlag" method.
You can implement like below in Activity.
private static boolean cFlag = false;
public static boolean getFlag()
return cFlag;
public static void setFlag(boolean cFlag)
StatusActivity.cFlag = cFlag;
and call from position class like below
public class Position
public boolean ccflag = StatusActivity.getFlag();
StatusActivity.setFlag(false);
edited Nov 14 '18 at 9:28
answered Nov 14 '18 at 7:32
gunavant patelgunavant patel
17618
17618
could you explain the right way with an example,it could have a greet help. thank you
– Shafeeq Mohammed
Nov 14 '18 at 8:31
Check my answer I was edited with an example.
– gunavant patel
Nov 14 '18 at 9:29
StatusActivity.setFlag(false); is showing error(cannot resolve symbol 'setFlag')
– Shafeeq Mohammed
Nov 14 '18 at 10:49
it worked :) thank you
– Shafeeq Mohammed
Nov 14 '18 at 11:16
add a comment |
could you explain the right way with an example,it could have a greet help. thank you
– Shafeeq Mohammed
Nov 14 '18 at 8:31
Check my answer I was edited with an example.
– gunavant patel
Nov 14 '18 at 9:29
StatusActivity.setFlag(false); is showing error(cannot resolve symbol 'setFlag')
– Shafeeq Mohammed
Nov 14 '18 at 10:49
it worked :) thank you
– Shafeeq Mohammed
Nov 14 '18 at 11:16
could you explain the right way with an example,it could have a greet help. thank you
– Shafeeq Mohammed
Nov 14 '18 at 8:31
could you explain the right way with an example,it could have a greet help. thank you
– Shafeeq Mohammed
Nov 14 '18 at 8:31
Check my answer I was edited with an example.
– gunavant patel
Nov 14 '18 at 9:29
Check my answer I was edited with an example.
– gunavant patel
Nov 14 '18 at 9:29
StatusActivity.setFlag(false); is showing error(cannot resolve symbol 'setFlag')
– Shafeeq Mohammed
Nov 14 '18 at 10:49
StatusActivity.setFlag(false); is showing error(cannot resolve symbol 'setFlag')
– Shafeeq Mohammed
Nov 14 '18 at 10:49
it worked :) thank you
– Shafeeq Mohammed
Nov 14 '18 at 11:16
it worked :) thank you
– Shafeeq Mohammed
Nov 14 '18 at 11:16
add a comment |
you can not instantiate Activity class. if you want to call a method from activity, fist you should check that activity already running and not destroyed then by having the context of your class you just cast it like below then use its method
StatusActivity statusactivity= (StatusActivity )context;
statusactivity.setFlag(false);
1
public boolean ccflag = statusactivity.getFlag(); this code is working for me
– Shafeeq Mohammed
Nov 14 '18 at 8:29
add a comment |
you can not instantiate Activity class. if you want to call a method from activity, fist you should check that activity already running and not destroyed then by having the context of your class you just cast it like below then use its method
StatusActivity statusactivity= (StatusActivity )context;
statusactivity.setFlag(false);
1
public boolean ccflag = statusactivity.getFlag(); this code is working for me
– Shafeeq Mohammed
Nov 14 '18 at 8:29
add a comment |
you can not instantiate Activity class. if you want to call a method from activity, fist you should check that activity already running and not destroyed then by having the context of your class you just cast it like below then use its method
StatusActivity statusactivity= (StatusActivity )context;
statusactivity.setFlag(false);
you can not instantiate Activity class. if you want to call a method from activity, fist you should check that activity already running and not destroyed then by having the context of your class you just cast it like below then use its method
StatusActivity statusactivity= (StatusActivity )context;
statusactivity.setFlag(false);
answered Nov 14 '18 at 7:36
AmirAmir
159214
159214
1
public boolean ccflag = statusactivity.getFlag(); this code is working for me
– Shafeeq Mohammed
Nov 14 '18 at 8:29
add a comment |
1
public boolean ccflag = statusactivity.getFlag(); this code is working for me
– Shafeeq Mohammed
Nov 14 '18 at 8:29
1
1
public boolean ccflag = statusactivity.getFlag(); this code is working for me
– Shafeeq Mohammed
Nov 14 '18 at 8:29
public boolean ccflag = statusactivity.getFlag(); this code is working for me
– Shafeeq Mohammed
Nov 14 '18 at 8:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294953%2fhow-do-i-call-one-method-of-an-activity-from-another-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the error you are seeing?
– Janwilx72
Nov 14 '18 at 7:22
3
Never instantiate an
Activity
yourself. This is the task of the framework, which will make all necessary initializations and call the activitie's life cycle methods. If you need an instance of the activity in your class pass it as a parameter.– Henry
Nov 14 '18 at 7:26
StatusActivity statusactivity = new StatusActivity();
is just plain wrong, instead of this, you should inject yourPosition
object into that activity and have control over it.– Jay
Nov 14 '18 at 7:27
We never create an instance of an activity by ourself. Its created by android framework work behind the scene. To call activitie's method from other class you need to pass reference of activity to that class
– Umer Farooq
Nov 14 '18 at 7:34
@Janwilx72 its showing cannot resolve symbol 'setFlag'
– Shafeeq Mohammed
Nov 14 '18 at 8:10