Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Essential Guide to Android Activities and Fragments

Tech 1

Creatnig an Activity in Android

  1. Extend the Activity class or its subclasses
  2. Declare the Activity in AndroidManifest.xml
  3. Create a layout and set it in Activity's onCreate method

Example manifest declaration:

<activity
    android:name=".TestActivity"
    android:exported="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Activity Lifecycle Methods

Key lifecycle callbacks:

Starting:
onCreate -> onStart -> onResume

Background:
onPause -> onStop

Returning:
onRestart -> onStart -> onResume

Exiting:
onPause -> onStop -> onDestroy

Activity Launch Modes

Four launch modes defined in manifest:

  1. standard (default) - Creates new instance each time
  2. singleTop - Reuses if already at top of stack
  3. singleTask - Reuses if exists in stack, clears above
  4. singleInstance - Global singleton in separtae stack

Example singleTop implementation:

<activity
    android:name=".MainActivity"
    android:launchMode="singleTop"
    android:exported="true">
</activity>

Fragment Fundamentals

Key characteristics:

  • Has independent lifecycle
  • Depends on host Activity
  • Can be retrieved via FragmentManager

Basic fragment creation:

public class SampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sample, container, false);
    }
}

Fragment Communication Patterns

  1. Public Methods:
// In Activity
public void updateData(String data) { /*...*/ }

// In Fragment
((HostActivity) getActivity()).updateData("New data");
  1. Interface Callbacks:
// In Fragment
public interface DataListener {
    void onDataReceived(String data);
}

// In Activity
public class MainActivity implements SampleFragment.DataListener {
    @Override
    public void onDataReceived(String data) { /*...*/ }
}

Fragment Back Stack Management

Proper back stack handling:

getParentFragmentManager().beginTransaction()
    .hide(currentFragment)
    .add(R.id.container, newFragment)
    .addToBackStack(null)
    .commit();
Tags: Android

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.