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
Related
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.ñ
I'm trying to call a method when a user launch my application (no matter it is a fresh launch, or a return to it after hiding by home buttton)
For iOS, we can put the method in "applicationDidBecomeActive" in AppDelegate.m, so the method will be called when app launches.
However, for Android, if we put the method in onResume() in the 1st activity, not only app launch will call the method, backing to the the 1st activity from other activities in the same app will also call the method. I don't want this happen, I just want the method to be called when app launches.
I've asked it in the past but seems no answer on Android.
Any solutions? Thanks a lot.
You can implement your own Application class. MyApplication extends Application and set it as your Application in the manifest file AndroidManifest.xml.
<application
android:name="MyApplication"
.
.
. >
</application>
In MyApplication Class, implement onCreate() and onTerminate() methods.
onCreate() method called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
See the docs for Application.
There is no method that is called only when the app returns from background, but you could implement something that you could implement something like this to see if the app started from background or it was first started. Create a general activity that will be extended by all the other activities and override onStart():
public abstract class CustomActivity extends FragmentActivity {
public static int ACTIVITIES_RUNNING = 0;
#Override
protected void onStart() {
super.onStart();
if (ACTIVITIES_RUNNING == 0) {
//app came from background
//start whatever you want
}
Const.ACTIVITIES_RUNNING++;
}
}
First create singleton for counting activities in foreground
public class ActivitiesCounter {
public interface ApplicationLaunchListener{
public void onLaunch();
}
private int mCounter = 0;
private ApplicationLaunchListener mListener;
private static ActivitiesCounter mInstance = new ActivitiesCounter();
public static ActivitiesCounter getInstance(){
return mInstance;
}
public void increase(){
if(mCounter == 0){
if(mListener != null){
mListener.onLaunch();
}
}
mCounter++;
}
public void decrease(){
mCounter--;
}
public void setApplicationLaunchListener(ApplicationLaunchListener listener){
mListener = listener;
}
}
Then activity:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActivitiesCounter.getInstance().setApplicationLaunchListener(new ActivitiesCounter.ApplicationLaunchListener() {
#Override
public void onLaunch() {
Toast.makeText(MyActivity.this, "launched", Toast.LENGTH_LONG).show();
}
});
findViewById(R.id.btn_activity_b).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MyActivity.this, ActivityB.class);
startActivity(intent);
}
});
}
#Override
protected void onStart() {
super.onStart();
ActivitiesCounter.getInstance().increase();
}
#Override
protected void onStop() {
ActivitiesCounter.getInstance().decrease();
super.onStop();
}
}
Activity B also should increase and decrease counter
public class ActivityB extends Activity {
#Override
protected void onStart() {
super.onStart();
ActivitiesCounter.getInstance().increase();
}
#Override
protected void onStop() {
ActivitiesCounter.getInstance().decrease();
super.onStop();
}
}
It's better create BaseActivity (with onStart, onStop) for all activities in your app (then, you don't need override onStart, onStop every time)
You can define a superclass for all your activities and track the state of the app. If all activities are in stopped state - app in the background, otherwise - in the foreground. In onStart() and onStop() methods of your super activity you can increment and decrement the number of visible activites. Then in onStart() check if there was any currently visible activites. If no - app becomes active and you can call your method:
public class SuperActivity extends Activity {
private static int mVisibleActivitiesCount;
#Override
public void onStart(){
super.onStart();
if (mVisibleActivitiesCount == 0) {
onAppBecomesActive();
}
mVisibleActivitiesCount++;
}
#Override
public void onStop(){
super.onStart();
mVisibleActivitiesCount--;
}
private void onAppBecomesActive() {
// Do some staff
}
}
Open the androidManifest.xml from the Manifests Folder
Choose the Activity you want to open when app start
Add these codes to that Activity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Remember to removes this code from the activity in which its already present(usually MainActivity)
I have a function named 'func()'. I want to run this function when application start without clicking any button. just when application load I want to show a massage.that massage in that function. I just want to run that function when app start what will be the code.
public class TextViewActivity extends Activity {
public static EditText etxt;
public final void func(){
etxt.setText("Massage");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
}
}
Just put a call to the function on the onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
func(); //A call to the function.
}
Hope that helps.
I don't recommend subclassing the Application in order to do this. When the application starts it will go to the main activity. So I would say just keep a SharedPreference boolean value if it has been set. If not show the message.
So keep state of the application here: http://developer.android.com/reference/android/content/SharedPreferences.html, just set a boolean. Remember when you app gets called, the activity onCreate always gets called of the main activity, so its just a matter of not calling it again.
try using below code.. you need to call ur function after you initialize edittext etxt. so it can not cause you NPE
public final void func(){
etxt.setText("Message");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
func();// here your function call.
}
Try this:
public class TextViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.etxt2).setText("SMTH");
}
}
In case you really need a function to be called, you can use this:
public class TextViewActivity extends Activity {
public final void func() {
findViewById(R.id.etxt2).setText("SMTH");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
func();
}
}
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