0

I have an Android Jetpack Compose application in Kotlin with a function requiring context. The function is as seen below.

fun copyDataBase(context: Context, OUTPUT_DB_PATH: String, DB_NAME: String) {
    // Path to output database path (application database path).
    var APP_DB_FILE_PATH = OUTPUT_DB_PATH + "/" + DB_NAME

    // Path to input database file.
    var INPUT_DB_PATH = "database/" + DB_NAME

    // Open your local db as the input stream
    val databaseInput: InputStream = context.getAssets().open(INPUT_DB_PATH)

    // Open the empty db as the output stream
    val databaseOutput: OutputStream = FileOutputStream(APP_DB_FILE_PATH)

    // Transfer bytes from the inputfile to the outputfile
    val buffer = ByteArray(1024)
    var length: Int
    while (databaseInput.read(buffer).also { length = it } > 0) {
        databaseOutput.write(buffer, 0, length)
    }

    // Close the streams
    databaseOutput.flush()
    databaseOutput.close()
    databaseInput.close()
}

And then in the main activity I do this.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // How can I get context to supply to the function?
        copyDataBase(context, DB_PATH, DB_NAME)

        setContent {
            Column() {
                TopAppBar(title = { Text(text = "Nyimbo") })
                ScrollableColumn(Modifier.fillMaxWidth()
                    .padding(10.dp)) {
                    lyricsPageComposable(lyricsMap = songMap)
                }
            }
        }
    }
}

How can I get context to supply it to the function in the main activity? From which Jetpack Compose namespace is it imported from?

I have tried import android.content.Context but this requires initialization of which I can't figure out how to do it.

The idea is to copy the pre-populated SQLite database from assets directory into the application at first run.

2
  • Cannot you call your db method after setting your content? Commented Nov 27, 2020 at 14:05
  • @jeelVankhede I'm not sure what you mean by `after setting your content'. The function requires the context to start with, no matter where it is called. Commented Nov 27, 2020 at 14:09

1 Answer 1

4

How can I get context to supply it to the function in the main activity?

Where you have that call, use this. An Activity is a Context.

Ideally, this code would not be in an activity — it would be in a repository, or at worst a viewmodel, with the actual disk I/O being performed on a background thread.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you so much. I'm learning Android development, could elaborate further, what do you mean by repository?
@Amani: FWIW, I strongly recommend that you put Jetpack Compose aside for a while and come back to it in, say, 2022. Compose is in an early alpha state right now. It is not all that easy for experienced Android developers to pick up, due to modest (and sometimes outdated) documentation and other resources. I cannot recommend it to newcomers to Android right now. Wait for it to become stable (hopefully in late 2021) and worry about it then. In the meantime, focus on learning Android using existing resources and the classic View system.
@divine: That depends on your definition of "ok". 😁 There is a stable release, and there are reports of developers using it for shipping apps. There are also reports of a fair number of bugs and limitations. Plus, while a fair bit has been written about Compose UI, it is still a tiny fraction of what has been written about the classic View system. Going by the technology adoption lifecycle, I would say we are in the "Innovators" section heading towards "Early Adopters". If you feel that fits you, start composing!
@divine: "is it possible to wrap a fragment or an activity inside a composable function?" -- no, sorry. You can define the UI of a fragment or an activity as a composable, but that is as close as you are going to come.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.