Android Development | Part 2 Architecture of the app
After learning basics of android framework about activities, fragments and its states, we decided to create first version of an app. It consisted of several activities which were designed to communicate with each other (e.g. sending ids or string names of chosen items). As it turned out, there is no need of creating separate activity, especially for a simple app like planner. Moreover, switching between two activities is longer than two fragments. More detailed debate activities vs fragments described in this StackOverflow question. Thus, it was decided to leave ourselves with one activity (basically "main activity") and several fragments for each page(e.g. home screen with rooms, room fragment, addItem and AddRoom fragments). Along with fragments it was suggested to add view adapters to accommodate standard android LinearLayout and GridLayout Views, thus creating the following hierarchy: In both cases(fragment-fragment, activity-activity, or activity-fragment, even activity-viewadapter) communication can be done via following procedures: To send some info via Intent from one :String YOUR_KEY = "id"; Intent intent = new Intent(getApplicationContext(), DisplayContact.class); intent.putExtra(YOUR_KEY, id); startActivity(intent);
Or via Bundle:
Bundle dataBundle = new Bundle(); dataBundle.putInt(YOUR_KEY, id); intent.putExtras(dataBundle);
And from the other end (on receiving side). Via intent:
int defaultValue = 0; int id = getIntent().getIntExtra(YOUR_KEY, defaultValue);
Bundle extras = getIntent().getExtras(); int id = extras.getInt(YOUR_KEY);

Comments
Post a Comment