Mobile App Architecture PDF
Document Details
Uploaded by SpotlessFunction
Tags
Summary
This document provides an overview of mobile app architecture, focusing on Android Activity Lifecycle. It details the different stages of the lifecycle and the methods used. The document covers important concepts like components, intent, and other relevant topics for android app development, which provides a key resource for developers.
Full Transcript
10/30/2024 Mobile App Architecture 1 What is Mobile App Architecture? Structure of an app’s core elements (data, presentation, navigation). 2 1 ...
10/30/2024 Mobile App Architecture 1 What is Mobile App Architecture? Structure of an app’s core elements (data, presentation, navigation). 2 1 10/30/2024 Why Mobile App Architecture? Affects app performance, maintainability, and scalability. Differences by Platform: Android: MVVM or Clean Architecture. iOS: MVC or MVVM. 3 Android Activity Lifecycle Manages user interactions and resource handling. Example: Pausing an activity when a call interrupts. 4 2 10/30/2024 Android Activity Lifecycle Lifecycle Stages: onCreate, onStart, onResume, onPause, onStop, onDestroy. 5 Lifecycle Stages and Methods: onCreate(Bundle savedInstanceState) Called when an Activity is first created. Initialization of the Activity, setting up UI, binding data, and initializing components. 6 3 10/30/2024 Lifecycle Stages and Methods: onStart(): Called after onCreate and made the Activity visible to the user but not yet in the front. Set up things that should be re-initiated every time the Activity becomes visible, such as registering receivers or starting animations. 7 Lifecycle Stages and Methods: onResume() Called when the Activity gains focus and becomes interactive. Start updates that need to occur every time the Activity is active, like starting animations, refreshing UI, or updating data. 8 4 10/30/2024 Lifecycle Stages and Methods: onPause() Called when the Activity is partially block, such as when a dialog appears or the user navigates away. Pause updates, animations, or audio playback, save data and commit changes. 9 Lifecycle Stages and Methods: onStop() Called when the Activity is no longer visible, often when the user opens a new Activity. 10 5 10/30/2024 Lifecycle Stages and Methods: onRestart() Called if an Activity is stopped and then restarted, such as when a user returns to the Activity from the Recent Apps menu. Use this to re-initialize any resources released in onStop. 11 Lifecycle Stages and Methods: onDestroy() Final method called before the Activity is destroyed, either due to user or system-initiated actions. Clean up any remaining resources, end background tasks, and perform any final state persistence if it has not already been handled. 12 6 10/30/2024 onCreate() protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("Lifecycle", "MainActivity: onCreate called"); } 13 onStart() protected void onStart() { super.onStart(); Log.d("Lifecycle", "MainActivity: onStart called"); } 14 7 10/30/2024 onResume() protected void onResume() { super.onResume(); Log.d("Lifecycle", "MainActivity: onResume called"); } 15 onPause() protected void onPause() { super.onPause(); Log.d("Lifecycle", "MainActivity: onPause called"); } 16 8 10/30/2024 onStop() protected void onStop() { super.onStop(); Log.d("Lifecycle", "MainActivity: onStop called"); } 17 onDestory() protected void onDestroy() { super.onDestroy(); Log.d("Lifecycle", "MainActivity: onDestroy called"); } 18 9 10/30/2024 Components of Android Apps - Activities An Activity in Android is a single screen with a user interface, like a web page on a website. Example: News feed, Editing a document, Navigating a map 19 Components of Android Apps - Activities An Activity in Android is a single screen with a user interface, like a web page on a website. Example: Home Activity: Shows a list of news articles. Detail Activity: Shows the full content of a selected article. Settings Activity: Allows the user to configure app settings. 20 10 10/30/2024 Purpose of Activities Display a user interface for user interaction. Handle user input and respond to touch, keyboard, or system events. Manage transitions between different screens in an app Act as an entry point into an app. When you launch an app, you start at its main Activity. 21 Activity Lifecycle onCreate(): Called when the Activity is first created. onStart(): The Activity becomes visible to the user but is not yet in the foreground. onResume(): The Activity becomes interactive in the foreground. onPause(): The Activity is about to lose focus, so it’s partially visible but not fully interactive. onStop(): The Activity is no longer visible when another Activity takes over the screen. onDestroy(): Called before the Activity is completely removed from memory. 22 11 10/30/2024 Components of Android Apps - Fragments A Fragment represents a reusable portion of an app's user interface within an Activity. Fragments are designed to be embedded within an Activity and occupy part or all of an Activity’s screen. Fragments allow us to create a modular UI design that can be reused across screens or app sections. For example: In a news app: You might have a Fragment for displaying a list of articles and another for showing the content of a selected article. In a settings screen: Each Fragment might represent a separate settings category, like account preferences or notification settings. 23 Purpose of Fragments Reusability Dynamic UI Design Simplified Navigation Lifecycle Awareness 24 12 10/30/2024 Components of Android Apps - Intents An Intent is an abstract description of an operation to be performed. Intents enable Android apps to do a wide variety of tasks, such as: Starting a new activity (screen) within the same or another application. Starting a service to perform background operations. Broadcasting a message to other apps. Receiving data or passing data between different components. 25 Types of Intents Explicit Intent Implicit Intent 26 13 10/30/2024 Explicit Intent An Explicit Intent is used to launch a specific component (like an Activity or Service) within the same application. Example: Intent intent = new Intent(this, SecondActivity.class); startActivity(intent); 27 Implicit Intent An Implicit Intent does not specify a specific component. Implicit Intents allow communication between different applications. Example: Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.example.com")); startActivity(intent); 28 14 10/30/2024 Passing Data with Intent Extras Intent intent = new Intent(this, SecondActivity.class); intent.putExtra("username", "JohnDoe"); startActivity(intent); Second Activity: String username = getIntent().getStringExtra("username"); 29 Implementing Activities and Fragments in Android public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ListFragment()).commit(); } 30 15 10/30/2024 Implementing Activities and Fragments in Android public class ListFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_list, container, false); Button detailsButton = view.findViewById(R.id.details_button); detailsButton.setOnClickListener(v -> { getParentFragmentManager().beginTransaction().replace(R.id.fragment_container, new DetailFragment()).addToBackStack(null).commit(); }); 31 Implementing Activities and Fragments in Android 32 16