oppia / oppia-android

A free, online & offline learning platform to make quality education accessible for all.
https://www.oppia.org
Apache License 2.0
311 stars 515 forks source link

Add a wiki page/section which shows best practices for creating a fragment/activitiy #1586

Open rt4914 opened 4 years ago

rt4914 commented 4 years ago

Best practice for fragment is to use companion object and pass information like:

companion object {
    /** Returns a new [StoryFragment] to display the story corresponding to the specified story ID. */
    fun newInstance(internalProfileId: Int, topicId: String, storyId: String): StoryFragment {
      val storyFragment = StoryFragment()
      val args = Bundle()
      args.putInt(KEY_INTERNAL_PROFILE_ID_ARGUMENT, internalProfileId)
      args.putString(KEY_TOPIC_ID_ARGUMENT, topicId)
      args.putString(KEY_STORY_ID_ARGUMENT, storyId)
      storyFragment.arguments = args
      return storyFragment
    }
  }

And best practice for Activity is also same as above.

  companion object {
    const val STORY_ACTIVITY_INTENT_EXTRA_INTERNAL_PROFILE_ID = "StoryActivity.internal_profile_id"
    const val STORY_ACTIVITY_INTENT_EXTRA_TOPIC_ID = "StoryActivity.topic_id"
    const val STORY_ACTIVITY_INTENT_EXTRA_STORY_ID = "StoryActivity.story_id"

    /** Returns a new [Intent] to route to [StoryActivity] for a specified story. */
    fun createStoryActivityIntent(
      context: Context,
      internalProfileId: Int,
      topicId: String,
      storyId: String
    ): Intent {
      val intent = Intent(context, StoryActivity::class.java)
      intent.putExtra(STORY_ACTIVITY_INTENT_EXTRA_INTERNAL_PROFILE_ID, internalProfileId)
      intent.putExtra(STORY_ACTIVITY_INTENT_EXTRA_TOPIC_ID, topicId)
      intent.putExtra(STORY_ACTIVITY_INTENT_EXTRA_STORY_ID, storyId)
      return intent
    }
  }

We should mention these coding practices in wiki.

adhiamboperes commented 8 months ago

For Fragment and Activity arguments, we are moving over to protos so the wiki should refllect that.

adhiamboperes commented 8 months ago

I think that this issue should also broadly cover setting up a new UI which includes adding the activity to Manifest, dagger's ActivityComponentImpl and fragment to FragmentComponentImpl.

Activities/Fragments should be accompanied by corresponding UI tests for the flows they contain.

We use view/databinding which sometimes necessitates use of viewmodels, so viewmodel creation best practices should be added.