Going Home Screen of app in android? - android

I want to go to homePage of my application on clicking button where i was in inner page.
Can any one tell me how to do that ?
Thanking you ,
Srinivas

I suggest creating an Application class. In that class have a boolean field that is false by default. Every Activity should check if that field is true in onResume() and call finish() if it is true (except for the main activity that always sets the field to false in onResume()). You can even create a custom Activity that does this and then have all activities extend that activity.
Resources:
http://d.android.com/resources/faq/framework.html#3
The android.app.Application class
The android.app.Application is a base class for those who need to maintain global application state. It can be accessed via getApplication() from any Activity or Service. It has a couple of life-cycle methods and will be instantiated by Android automatically if your register it in AndroidManifest.xml.
Reference: http://d.android.com/reference/android/app/Application.html
Stripped-down example:
MyApp
public class MyApp extends Application { public boolean goBack = false; }
MyActivity
public class MyActivity extends Activity {
protected void onResume() {
if ( ((MyApp) getApplication()).goBack ) finish();
}
}
SomeActivity
public class SomeActivity extends MyActivity {
// nothing special here, it's all been implemented in MyActivity!
}
MainActivity
public class MainActivity extends Activity {
protected void onResume() {
((MyApp) getApplication()).goBack = false;
}
}
AndroidManifest.xml
[...] <application android:name=".MyApp" [...]> [...]
Note: You don't need to declare MyActivity in AndroidManifest.xml because it will never be launched directly (it will only be extended).

On your button click event add the following lines of code
moveTaskToBack(true);

There are probably two ways of doing that (general concepts):
The first one would be to simply launch the home actvity again, if you don't mind having it re-created again, unless that activity is a "singleTask" or "singleInstance" activity.
The second one would be to close the top activity in the stack as long as it is not your home activity. I don't see an easy way to achieve that, maybe by finishing the current activity with a specific result that gets checked by the launching activity, who will in turn close and send the result until the home activity is reached.

Related

Android landscape orientation on ALL activities

I would like to have an appwide rule to not allow the landscape orientation. I realize I could get this by putting:
android:screenOrientation="portrait"
in every one of my activities but that doesn't seem clean. I can also put
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
in every one of my activity's onCreate() methods.
But I desire a solution that doesn't require this code duplication. Any suggestions?
Solution:
You can do this for your entire application without having to make all your activities extend a common base class.
The trick is first to make sure you include an Application subclass in your project. In its onCreate(), called when your app first starts up, you register an ActivityLifecycleCallbacks object (API level 14+) to receive notifications of activity lifecycle events.
This gives you the opportunity to execute your own code whenever any activity in your app is started (or stopped, or resumed, or whatever). At this point you can call setRequestedOrientation() on the newly created activity.
class MyApp extends Application {
#Override
public void onCreate() {
super.onCreate();
// register to be informed of activities starting up
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
#Override
public void onActivityCreated(Activity activity,
Bundle savedInstanceState) {
// new activity created; force its orientation to portrait
activity.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
....
});
}
}
Any doubts, Please leave a comment.
Note: Add in manifest's <application> tag: android:name=".MyApp"
Hope it helps.

Disabling back button on multiple activities (Android)

I have seen this question:
Disable back button in android
(Please do not mark as duplicate for this.)
My query is this:
I have twenty activities in a row. I want to disable the back button, so that the user can never come back to the activity he once crosses. Currently, how I do this is override onBackPressed() and remove the super.onBackPressed() call. This works fine.
I now need to add forty more activities, and it should have the same effect. Is there a method where I can just disable the back button for the entire application without having to code it up in each Activity?
Create BaseActivity and extend every your Activity with this BaseActivity, and add onBackPressed() logic in BaseActivity.
Ex:
public class BaseActivity extends AppCompatActivity {
// Add your onBackPressed() logic here
}
Your activity,
public class MyActivityA extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity);
}
}
U can achieve this by finishing the pervious activity while going to next activity.

getSimpleName from a BaseActivity after finish()

in my App I created a BaseActivity, from which I extend all my Activities.
Inside the BaseActivity I recognize the activity that is running through the following statement: this.getClass().getSimpleName() (ex: ActivityA)
Until I open new Activity (ActivityB, ActivityC, etc ...) everything works correctly.
The problem occurs when I use the back button of the phone (I get back from activityC to ActivityB). In this case the ActivityB is properly resumed from the Stack, but in BaseTable (of ActivityB) the value of this.getClass().getSimpleName() remains ActivityC.
How can I avoid this problem? You know you help me?
All you need to do is create a protected final String in your BaseActivity as follows...
public class MyBaseActivity extends Activity {
protected final String TAG = getClass().getSimpleName();
// Any other code here
}
Any Activity which extends MyBaseActivity will inherit the TAG field and it will be instantiated with the correct name at the time it's created.
I put a TAG field in all of my Android base classes so I can use it with Log to identify different objects when they log to logcat.

Access an Activity from a Fragment plugged into another Activity

I have been using this example as a base for my project.
I have changed the project to use an ArrayAdapter for the titles-fragment's ListItems and changed the DetailsFragment's View to display a custom layout, which as a Button that is supposed to add an entry into a database.
Instead of generating widgets like in the example, I just inflate a custom XML into the FrameLayout besides the TitlesFragment in the 'layout-land' version of the layout.
My problem stems from the fact that DetailsFragment is plugged into the MainActivity when in Landscape mode but gets it's own DetailsActivty if it is in Portrait mode.
The Button I have in my custom layout for the details-fragment calls a function in its onClick() that is called AddNewItem.
So when the Button was clicked in Landscape mode, it crashed, because there was no AddNewItem in the MainActivity. I solved this by using a BroadcastReceiver, so when the button is clicked, a method named AddNewItem in MainActivity.java instead broadcasts a custom event and i have a BroadcastReceiver that calls the AddNewItem in DetailsFragment.java.
It looks like this:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
DetailsActivity m = (DetailsActivity) DetailsActivity.getActivityInstance(); // see below for what getActivityInstance does
LayoutInflater mInf = LayoutInflater.from(context);
View myView = mInf.inflate(R.layout.customlayout, null);
((DetailsActivity) m).AddNewItem(myView);
}
}
But this gives me:
08-11 13:37:50.687: E/AndroidRuntime(6766): java.lang.RuntimeException: Unable to start receiver in.falkeninc.umt_v0_9_8_1.MyReceiver: java.lang.NullPointerException
I am not sure what is happening. Because I am also using a static variable in DetailsActivity.java to be able to reach it inside the BroadcastReceiver. The code looks like this:
public class DetailsActivity extends SherlockFragmentActivity {
...
public static SherlockFragmentActivity activityInstance;
...
activityInstance = this; // in the onCreate
...
public static SherlockFragmentActivity getActivityInstance(){
return activityInstance;
}
}
My problem stems from the fact that DetailsFragment is plugged into
the MainActivity when in Landscape mode but gets it's own
DetailsActivty if it is in Portrait mode.
This shouldn't be a problem. Although you have two activities you have the same DetailsFragment class in both of them. If a Button from that fragment is doing something then you should keep it at that fragment's level(that would be an ideal fragment, one that is self contained, a fragment that doesn't know or care where is put). If you do require the activity for the work in that method, you should add extra details.
Also, if the behavior is common to both activities you could make a base activity holding that method and let the two current activities inherit from that, so it will be available to the fragment no matter what.
public static SherlockFragmentActivity activityInstance;
Don't keep static references to activities, you risk leaking them.
Also, don't access activities from outside their own context. When an activity is not the one interacting with the user(it's onPause() has been called) that activity could as well be completely destroyed and trying to access it in another activity could bring you lots of problems.

Define Launcher Activity In Code Before Activities Loaded

I realise you can set the LAUNCHER activity of your app in the manifest file, but is there anyway you can statically do this in code before the activity is loaded by the Dalvik VM? Something like:
public class MyActivity extends Activity{
RunTime.LAUNCHER = MyActivity.class
...
}
I realise this might not be possible, but if it is I would appreciate a safe and reliable code example to achieve this?
Many thanks
What is possible, however, is to have a first empty activity that starts whatever activity you need next, without displaying itself.
public void onCreate(Bundle stuff) {
super.onCreate(stuff);
startActivity(new Intent(...whatever...);
finish();
}

Categories

Resources