Android Activities

This post contain my all activity related snippet.



#Start new Activity
staring a new activity is very simple. create an intent and pass that intent to startActivity


1
2
Intent i = new Intent(getBaseContext(), ConverterActivity.class);
                startActivity(i);

#Changing Title
set android:label attribute in the activity element


1
2
3
4
5
6
7
<activity android:name=".AboutActivity"
            android:label="About">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

            </intent-filter>
        </activity>

#Close one Activity when switching to other
It is need when you want your previous activity should close when new one is opened

Add "android:noHistory="true"" to activity in activity element


<activity android:name="skd.app.currencyconverter.MainActivity"    android:label="Currency Converter"    android:noHistory="true">



# Android disable the BackButton
Some time it is need to disable the back button press on the activity to do this you simply needed to override the onBackPressed() methods on your activity



public class Correct_answer extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_correct_answer);



    }

    //just override this and comment the super method
    @Override
    public void onBackPressed() {
       // super.onBackPressed();
    }
}

# Close current activity
just need to call method finish()


this.finish();


Comments